Version 1
[yaffs-website] / web / core / modules / views / src / Plugin / Derivative / ViewsEntityArgumentValidator.php
1 <?php
2
3 namespace Drupal\views\Plugin\Derivative;
4
5 use Drupal\Component\Plugin\Derivative\DeriverBase;
6 use Drupal\Core\Entity\EntityManagerInterface;
7 use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
8 use Drupal\Core\StringTranslation\StringTranslationTrait;
9 use Drupal\Core\StringTranslation\TranslationInterface;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11
12 /**
13  * Provides views argument validator plugin definitions for all entity types.
14  *
15  * @ingroup views_argument_validator_plugins
16  *
17  * @see \Drupal\views\Plugin\views\argument_validator\Entity
18  */
19 class ViewsEntityArgumentValidator extends DeriverBase implements ContainerDeriverInterface {
20   use StringTranslationTrait;
21
22   /**
23    * The base plugin ID this derivative is for.
24    *
25    * @var string
26    */
27   protected $basePluginId;
28
29   /**
30    * The entity manager.
31    *
32    * @var \Drupal\Core\Entity\EntityManagerInterface
33    */
34   protected $entityManager;
35
36   /**
37    * List of derivative definitions.
38    *
39    * @var array
40    */
41   protected $derivatives = [];
42
43   /**
44    * Constructs an ViewsEntityArgumentValidator object.
45    *
46    * @param string $base_plugin_id
47    *   The base plugin ID.
48    * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
49    *   The entity manager.
50    * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
51    *   The string translation.
52    */
53   public function __construct($base_plugin_id, EntityManagerInterface $entity_manager, TranslationInterface $string_translation) {
54     $this->basePluginId = $base_plugin_id;
55     $this->entityManager = $entity_manager;
56     $this->stringTranslation = $string_translation;
57   }
58
59   /**
60    * {@inheritdoc}
61    */
62   public static function create(ContainerInterface $container, $base_plugin_id) {
63     return new static(
64       $base_plugin_id,
65       $container->get('entity.manager'),
66       $container->get('string_translation')
67     );
68   }
69
70   /**
71    * {@inheritdoc}
72    */
73   public function getDerivativeDefinitions($base_plugin_definition) {
74     $entity_types = $this->entityManager->getDefinitions();
75     $this->derivatives = [];
76     foreach ($entity_types as $entity_type_id => $entity_type) {
77       $this->derivatives[$entity_type_id] = [
78         'id' => 'entity:' . $entity_type_id,
79         'provider' => 'views',
80         'title' => $entity_type->getLabel(),
81         'help' => $this->t('Validate @label', ['@label' => $entity_type->getLabel()]),
82         'entity_type' => $entity_type_id,
83         'class' => $base_plugin_definition['class'],
84       ];
85     }
86
87     return $this->derivatives;
88   }
89
90 }