3 namespace Drupal\views\Plugin\views\filter;
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;
12 * Filter class which allows filtering by entity bundles.
14 * @ingroup views_filter_handlers
16 * @ViewsFilter("bundle")
18 class Bundle extends InOperator {
21 * The entity type for the filter.
25 protected $entityTypeId;
28 * The entity type definition.
30 * @var \Drupal\Core\Entity\EntityTypeInterface
32 protected $entityType;
37 * @var \Drupal\Core\Entity\EntityManagerInterface
39 protected $entityManager;
42 * The bundle info service.
44 * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
46 protected $bundleInfoService;
49 * Constructs a Bundle object.
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
59 * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $bundle_info_service
60 * The bundle info service.
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);
65 $this->entityManager = $entity_manager;
66 $this->bundleInfoService = $bundle_info_service;
72 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
77 $container->get('entity.manager'),
78 $container->get('entity_type.bundle.info')
85 public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
86 parent::init($view, $display, $options);
88 $this->entityTypeId = $this->getEntityType();
89 $this->entityType = \Drupal::entityManager()->getDefinition($this->entityTypeId);
90 $this->real_field = $this->entityType->getKey('bundle');
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()]);
102 foreach ($types as $type => $info) {
103 $options[$type] = $info['label'];
107 $this->valueOptions = $options;
110 return $this->valueOptions;
116 public function query() {
117 // Make sure that the entity base table is in the query.
118 $this->ensureMyTable();
125 public function calculateDependencies() {
126 $dependencies = parent::calculateDependencies();
128 $bundle_entity_type = $this->entityType->getBundleEntityType();
129 $bundle_entity_storage = $this->entityManager->getStorage($bundle_entity_type);
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();
137 return $dependencies;