Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / views / src / Plugin / Derivative / ViewsExposedFilterBlock.php
1 <?php
2
3 namespace Drupal\views\Plugin\Derivative;
4
5 use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
6 use Drupal\Core\Entity\EntityStorageInterface;
7 use Symfony\Component\DependencyInjection\ContainerInterface;
8
9 /**
10  * Provides block plugin definitions for all Views exposed filters.
11  *
12  * @see \Drupal\views\Plugin\Block\ViewsExposedFilterBlock
13  */
14 class ViewsExposedFilterBlock implements ContainerDeriverInterface {
15
16   /**
17    * List of derivative definitions.
18    *
19    * @var array
20    */
21   protected $derivatives = [];
22
23   /**
24    * The view storage.
25    *
26    * @var \Drupal\Core\Entity\EntityStorageInterface
27    */
28   protected $viewStorage;
29
30   /**
31    * The base plugin ID that the derivative is for.
32    *
33    * @var string
34    */
35   protected $basePluginId;
36
37   /**
38    * Constructs a ViewsExposedFilterBlock object.
39    *
40    * @param string $base_plugin_id
41    *   The base plugin ID.
42    * @param \Drupal\Core\Entity\EntityStorageInterface $view_storage
43    *   The entity storage to load views.
44    */
45   public function __construct($base_plugin_id, EntityStorageInterface $view_storage) {
46     $this->basePluginId = $base_plugin_id;
47     $this->viewStorage = $view_storage;
48   }
49
50   /**
51    * {@inheritdoc}
52    */
53   public static function create(ContainerInterface $container, $base_plugin_id) {
54     return new static(
55       $base_plugin_id,
56       $container->get('entity.manager')->getStorage('view')
57     );
58   }
59
60   /**
61    * {@inheritdoc}
62    */
63   public function getDerivativeDefinition($derivative_id, $base_plugin_definition) {
64     if (!empty($this->derivatives) && !empty($this->derivatives[$derivative_id])) {
65       return $this->derivatives[$derivative_id];
66     }
67     $this->getDerivativeDefinitions($base_plugin_definition);
68     return $this->derivatives[$derivative_id];
69   }
70
71   /**
72    * {@inheritdoc}
73    */
74   public function getDerivativeDefinitions($base_plugin_definition) {
75     // Check all Views for displays with an exposed filter block.
76     foreach ($this->viewStorage->loadMultiple() as $view) {
77       // Do not return results for disabled views.
78       if (!$view->status()) {
79         continue;
80       }
81       $executable = $view->getExecutable();
82       $executable->initDisplay();
83       foreach ($executable->displayHandlers as $display) {
84         if (isset($display) && $display->getOption('exposed_block')) {
85           // Add a block definition for the block.
86           if ($display->usesExposedFormInBlock()) {
87             $delta = $view->id() . '-' . $display->display['id'];
88             $desc = t('Exposed form: @view-@display_id', ['@view' => $view->id(), '@display_id' => $display->display['id']]);
89             $this->derivatives[$delta] = [
90               'admin_label' => $desc,
91               'config_dependencies' => [
92                 'config' => [
93                   $view->getConfigDependencyName(),
94                 ],
95               ],
96             ];
97             $this->derivatives[$delta] += $base_plugin_definition;
98           }
99         }
100       }
101     }
102     return $this->derivatives;
103   }
104
105 }