Bundle.php 3.72 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\views\Plugin\views\filter;

5
use Drupal\Core\Entity\EntityManagerInterface;
6
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
7 8
use Drupal\views\ViewExecutable;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
9
use Symfony\Component\DependencyInjection\ContainerInterface;
10 11 12 13 14 15

/**
 * Filter class which allows filtering by entity bundles.
 *
 * @ingroup views_filter_handlers
 *
16
 * @ViewsFilter("bundle")
17 18 19 20 21 22 23 24
 */
class Bundle extends InOperator {

  /**
   * The entity type for the filter.
   *
   * @var string
   */
25
  protected $entityTypeId;
26 27

  /**
28
   * The entity type definition.
29
   *
30
   * @var \Drupal\Core\Entity\EntityTypeInterface
31
   */
32
  protected $entityType;
33

34 35 36 37 38 39 40
  /**
   * The entity manager.
   *
   * @var \Drupal\Core\Entity\EntityManagerInterface
   */
  protected $entityManager;

41 42 43
  /**
   * The bundle info service.
   *
44
   * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
45 46 47
   */
  protected $bundleInfoService;

48 49 50 51 52 53 54 55 56 57 58
  /**
   * Constructs a Bundle object.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
   *   The entity manager.
59 60
   * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $bundle_info_service
   *   The bundle info service.
61
   */
62
  public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityManagerInterface $entity_manager, EntityTypeBundleInfoInterface $bundle_info_service) {
63 64 65
    parent::__construct($configuration, $plugin_id, $plugin_definition);

    $this->entityManager = $entity_manager;
66
    $this->bundleInfoService = $bundle_info_service;
67 68 69 70 71 72 73 74 75 76
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
77 78
      $container->get('entity.manager'),
      $container->get('entity_type.bundle.info')
79 80 81
    );
  }

82
  /**
83
   * {@inheritdoc}
84 85 86 87
   */
  public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
    parent::init($view, $display, $options);

88 89 90
    $this->entityTypeId = $this->getEntityType();
    $this->entityType = \Drupal::entityManager()->getDefinition($this->entityTypeId);
    $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 103 104 105 106
      foreach ($types as $type => $info) {
        $options[$type] = $info['label'];
      }

      asort($options);
107
      $this->valueOptions = $options;
108 109
    }

110
    return $this->valueOptions;
111 112 113
  }

  /**
114
   * {@inheritdoc}
115 116 117 118 119 120 121
   */
  public function query() {
    // Make sure that the entity base table is in the query.
    $this->ensureMyTable();
    parent::query();
  }

122 123 124 125 126 127 128 129 130 131
  /**
   * {@inheritdoc}
   */
  public function calculateDependencies() {
    $dependencies = parent::calculateDependencies();

    $bundle_entity_type = $this->entityType->getBundleEntityType();
    $bundle_entity_storage = $this->entityManager->getStorage($bundle_entity_type);

    foreach (array_keys($this->value) as $bundle) {
132 133 134
      if ($bundle_entity = $bundle_entity_storage->load($bundle)) {
        $dependencies[$bundle_entity->getConfigDependencyKey()][] = $bundle_entity->getConfigDependencyName();
      }
135 136 137 138 139
    }

    return $dependencies;
  }

140
}