5114c40f8ea08f54b53949a6cad01355f07d9841
[yaffs-website] / web / core / lib / Drupal / Core / Entity / Plugin / DataType / Deriver / EntityDeriver.php
1 <?php
2
3 namespace Drupal\Core\Entity\Plugin\DataType\Deriver;
4
5 use Drupal\Core\Entity\EntityManagerInterface;
6 use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
7 use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
8 use Symfony\Component\DependencyInjection\ContainerInterface;
9
10 /**
11  * Provides data type plugins for each existing entity type and bundle.
12  */
13 class EntityDeriver implements ContainerDeriverInterface {
14
15   /**
16    * List of derivative definitions.
17    *
18    * @var array
19    */
20   protected $derivatives = [];
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    * The bundle info service.
38    *
39    * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
40    */
41   protected $bundleInfoService;
42
43   /**
44    * Constructs an EntityDeriver 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    */
51   public function __construct($base_plugin_id, EntityManagerInterface $entity_manager, EntityTypeBundleInfoInterface $bundle_info_service) {
52     $this->basePluginId = $base_plugin_id;
53     $this->entityManager = $entity_manager;
54     $this->bundleInfoService = $bundle_info_service;
55   }
56
57   /**
58    * {@inheritdoc}
59    */
60   public static function create(ContainerInterface $container, $base_plugin_id) {
61     return new static(
62       $base_plugin_id,
63       $container->get('entity.manager'),
64       $container->get('entity_type.bundle.info')
65     );
66   }
67
68   /**
69    * {@inheritdoc}
70    */
71   public function getDerivativeDefinition($derivative_id, $base_plugin_definition) {
72     if (!empty($this->derivatives) && !empty($this->derivatives[$derivative_id])) {
73       return $this->derivatives[$derivative_id];
74     }
75     $this->getDerivativeDefinitions($base_plugin_definition);
76     if (isset($this->derivatives[$derivative_id])) {
77       return $this->derivatives[$derivative_id];
78     }
79   }
80
81   /**
82    * {@inheritdoc}
83    */
84   public function getDerivativeDefinitions($base_plugin_definition) {
85     // Also keep the 'entity' defined as is.
86     $this->derivatives[''] = $base_plugin_definition;
87     // Add definitions for each entity type and bundle.
88     foreach ($this->entityManager->getDefinitions() as $entity_type_id => $entity_type) {
89       $this->derivatives[$entity_type_id] = [
90         'label' => $entity_type->getLabel(),
91         'constraints' => $entity_type->getConstraints(),
92         'internal' => $entity_type->isInternal(),
93       ] + $base_plugin_definition;
94
95       // Incorporate the bundles as entity:$entity_type:$bundle, if any.
96       foreach ($this->bundleInfoService->getBundleInfo($entity_type_id) as $bundle => $bundle_info) {
97         if ($bundle !== $entity_type_id) {
98           $this->derivatives[$entity_type_id . ':' . $bundle] = [
99             'label' => $bundle_info['label'],
100             'constraints' => $this->derivatives[$entity_type_id]['constraints']
101           ] + $base_plugin_definition;
102         }
103       }
104     }
105     return $this->derivatives;
106   }
107
108 }