Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / views / src / Plugin / views / filter / Bundle.php
1 <?php
2
3 namespace Drupal\views\Plugin\views\filter;
4
5 use Drupal\Core\Entity\EntityManagerInterface;
6 use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
7 use Drupal\views\ViewExecutable;
8 use Drupal\views\Plugin\views\display\DisplayPluginBase;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
10
11 /**
12  * Filter class which allows filtering by entity bundles.
13  *
14  * @ingroup views_filter_handlers
15  *
16  * @ViewsFilter("bundle")
17  */
18 class Bundle extends InOperator {
19
20   /**
21    * The entity type for the filter.
22    *
23    * @var string
24    */
25   protected $entityTypeId;
26
27   /**
28    * The entity type definition.
29    *
30    * @var \Drupal\Core\Entity\EntityTypeInterface
31    */
32   protected $entityType;
33
34   /**
35    * The entity manager.
36    *
37    * @var \Drupal\Core\Entity\EntityManagerInterface
38    */
39   protected $entityManager;
40
41   /**
42    * The bundle info service.
43    *
44    * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
45    */
46   protected $bundleInfoService;
47
48   /**
49    * Constructs a Bundle object.
50    *
51    * @param array $configuration
52    *   A configuration array containing information about the plugin instance.
53    * @param string $plugin_id
54    *   The plugin_id for the plugin instance.
55    * @param mixed $plugin_definition
56    *   The plugin implementation definition.
57    * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
58    *   The entity manager.
59    * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $bundle_info_service
60    *   The bundle info service.
61    */
62   public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityManagerInterface $entity_manager, EntityTypeBundleInfoInterface $bundle_info_service) {
63     parent::__construct($configuration, $plugin_id, $plugin_definition);
64
65     $this->entityManager = $entity_manager;
66     $this->bundleInfoService = $bundle_info_service;
67   }
68
69   /**
70    * {@inheritdoc}
71    */
72   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
73     return new static(
74       $configuration,
75       $plugin_id,
76       $plugin_definition,
77       $container->get('entity.manager'),
78       $container->get('entity_type.bundle.info')
79     );
80   }
81
82   /**
83    * {@inheritdoc}
84    */
85   public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
86     parent::init($view, $display, $options);
87
88     $this->entityTypeId = $this->getEntityType();
89     $this->entityType = \Drupal::entityManager()->getDefinition($this->entityTypeId);
90     $this->real_field = $this->entityType->getKey('bundle');
91   }
92
93   /**
94    * {@inheritdoc}
95    */
96   public function getValueOptions() {
97     if (!isset($this->valueOptions)) {
98       $types = $this->bundleInfoService->getBundleInfo($this->entityTypeId);
99       $this->valueTitle = $this->t('@entity types', ['@entity' => $this->entityType->getLabel()]);
100
101       $options = [];
102       foreach ($types as $type => $info) {
103         $options[$type] = $info['label'];
104       }
105
106       asort($options);
107       $this->valueOptions = $options;
108     }
109
110     return $this->valueOptions;
111   }
112
113   /**
114    * {@inheritdoc}
115    */
116   public function query() {
117     // Make sure that the entity base table is in the query.
118     $this->ensureMyTable();
119     parent::query();
120   }
121
122   /**
123    * {@inheritdoc}
124    */
125   public function calculateDependencies() {
126     $dependencies = parent::calculateDependencies();
127
128     $bundle_entity_type = $this->entityType->getBundleEntityType();
129     $bundle_entity_storage = $this->entityManager->getStorage($bundle_entity_type);
130
131     foreach (array_keys($this->value) as $bundle) {
132       if ($bundle_entity = $bundle_entity_storage->load($bundle)) {
133         $dependencies[$bundle_entity->getConfigDependencyKey()][] = $bundle_entity->getConfigDependencyName();
134       }
135     }
136
137     return $dependencies;
138   }
139
140 }