annotate core/modules/views/src/Plugin/Derivative/ViewsBlock.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\views\Plugin\Derivative;
Chris@0 4
Chris@0 5 use Drupal\Core\Entity\EntityStorageInterface;
Chris@0 6 use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
Chris@0 7 use Drupal\Core\StringTranslation\TranslatableMarkup;
Chris@0 8 use Symfony\Component\DependencyInjection\ContainerInterface;
Chris@0 9
Chris@0 10 /**
Chris@0 11 * Provides block plugin definitions for all Views block displays.
Chris@0 12 *
Chris@0 13 * @see \Drupal\views\Plugin\Block\ViewsBlock
Chris@0 14 */
Chris@0 15 class ViewsBlock implements ContainerDeriverInterface {
Chris@0 16
Chris@0 17 /**
Chris@0 18 * List of derivative definitions.
Chris@0 19 *
Chris@0 20 * @var array
Chris@0 21 */
Chris@0 22 protected $derivatives = [];
Chris@0 23
Chris@0 24 /**
Chris@0 25 * The base plugin ID.
Chris@0 26 *
Chris@0 27 * @var string
Chris@0 28 */
Chris@0 29 protected $basePluginId;
Chris@0 30
Chris@0 31 /**
Chris@0 32 * The view storage.
Chris@0 33 *
Chris@0 34 * @var \Drupal\Core\Entity\EntityStorageInterface
Chris@0 35 */
Chris@0 36 protected $viewStorage;
Chris@0 37
Chris@0 38 /**
Chris@0 39 * {@inheritdoc}
Chris@0 40 */
Chris@0 41 public static function create(ContainerInterface $container, $base_plugin_id) {
Chris@0 42 return new static(
Chris@0 43 $base_plugin_id,
Chris@0 44 $container->get('entity.manager')->getStorage('view')
Chris@0 45 );
Chris@0 46 }
Chris@0 47
Chris@0 48 /**
Chris@0 49 * Constructs a ViewsBlock object.
Chris@0 50 *
Chris@0 51 * @param string $base_plugin_id
Chris@0 52 * The base plugin ID.
Chris@0 53 * @param \Drupal\Core\Entity\EntityStorageInterface $view_storage
Chris@0 54 * The entity storage to load views.
Chris@0 55 */
Chris@0 56 public function __construct($base_plugin_id, EntityStorageInterface $view_storage) {
Chris@0 57 $this->basePluginId = $base_plugin_id;
Chris@0 58 $this->viewStorage = $view_storage;
Chris@0 59 }
Chris@0 60
Chris@0 61 /**
Chris@0 62 * {@inheritdoc}
Chris@0 63 */
Chris@0 64 public function getDerivativeDefinition($derivative_id, $base_plugin_definition) {
Chris@0 65 if (!empty($this->derivatives) && !empty($this->derivatives[$derivative_id])) {
Chris@0 66 return $this->derivatives[$derivative_id];
Chris@0 67 }
Chris@0 68 $this->getDerivativeDefinitions($base_plugin_definition);
Chris@0 69 return $this->derivatives[$derivative_id];
Chris@0 70 }
Chris@0 71
Chris@0 72 /**
Chris@0 73 * {@inheritdoc}
Chris@0 74 */
Chris@0 75 public function getDerivativeDefinitions($base_plugin_definition) {
Chris@0 76 // Check all Views for block displays.
Chris@0 77 foreach ($this->viewStorage->loadMultiple() as $view) {
Chris@0 78 // Do not return results for disabled views.
Chris@0 79 if (!$view->status()) {
Chris@0 80 continue;
Chris@0 81 }
Chris@0 82 $executable = $view->getExecutable();
Chris@0 83 $executable->initDisplay();
Chris@0 84 foreach ($executable->displayHandlers as $display) {
Chris@0 85 /** @var \Drupal\views\Plugin\views\display\DisplayPluginInterface $display */
Chris@0 86 // Add a block plugin definition for each block display.
Chris@0 87 if (isset($display) && !empty($display->definition['uses_hook_block'])) {
Chris@0 88 $delta = $view->id() . '-' . $display->display['id'];
Chris@0 89
Chris@0 90 $admin_label = $display->getOption('block_description');
Chris@0 91 if (empty($admin_label)) {
Chris@0 92 if ($display->display['display_title'] == $display->definition['title']) {
Chris@0 93 $admin_label = $view->label();
Chris@0 94 }
Chris@0 95 else {
Chris@0 96 // Allow translators to control the punctuation. Plugin
Chris@0 97 // definitions get cached, so use TranslatableMarkup() instead of
Chris@0 98 // t() to avoid double escaping when $admin_label is rendered
Chris@0 99 // during requests that use the cached definition.
Chris@0 100 $admin_label = new TranslatableMarkup('@view: @display', ['@view' => $view->label(), '@display' => $display->display['display_title']]);
Chris@0 101 }
Chris@0 102 }
Chris@0 103
Chris@0 104 $this->derivatives[$delta] = [
Chris@0 105 'category' => $display->getOption('block_category'),
Chris@0 106 'admin_label' => $admin_label,
Chris@0 107 'config_dependencies' => [
Chris@0 108 'config' => [
Chris@0 109 $view->getConfigDependencyName(),
Chris@0 110 ],
Chris@0 111 ],
Chris@0 112 ];
Chris@0 113
Chris@0 114 // Look for arguments and expose them as context.
Chris@0 115 foreach ($display->getHandlers('argument') as $argument_name => $argument) {
Chris@0 116 /** @var \Drupal\views\Plugin\views\argument\ArgumentPluginBase $argument */
Chris@0 117 if ($context_definition = $argument->getContextDefinition()) {
Chris@18 118 $this->derivatives[$delta]['context_definitions'][$argument_name] = $context_definition;
Chris@0 119 }
Chris@0 120 }
Chris@0 121
Chris@0 122 $this->derivatives[$delta] += $base_plugin_definition;
Chris@0 123 }
Chris@0 124 }
Chris@0 125 }
Chris@0 126 return $this->derivatives;
Chris@0 127 }
Chris@0 128
Chris@0 129 }