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