38ac960cc17a375b8adfe854d4a3ea06175cfbab
[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\Config\Entity\ConfigEntityInterface;
6 use Drupal\Core\Entity\EntityManagerInterface;
7 use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
8 use Drupal\Core\Entity\Plugin\DataType\ConfigEntityAdapter;
9 use Drupal\Core\Entity\Plugin\DataType\EntityAdapter;
10 use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
12
13 /**
14  * Provides data type plugins for each existing entity type and bundle.
15  */
16 class EntityDeriver implements ContainerDeriverInterface {
17
18   /**
19    * List of derivative definitions.
20    *
21    * @var array
22    */
23   protected $derivatives = [];
24
25   /**
26    * The base plugin ID this derivative is for.
27    *
28    * @var string
29    */
30   protected $basePluginId;
31
32   /**
33    * The entity manager.
34    *
35    * @var \Drupal\Core\Entity\EntityManagerInterface
36    */
37   protected $entityManager;
38
39   /**
40    * The bundle info service.
41    *
42    * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
43    */
44   protected $bundleInfoService;
45
46   /**
47    * Constructs an EntityDeriver object.
48    *
49    * @param string $base_plugin_id
50    *   The base plugin ID.
51    * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
52    *   The entity manager.
53    */
54   public function __construct($base_plugin_id, EntityManagerInterface $entity_manager, EntityTypeBundleInfoInterface $bundle_info_service) {
55     $this->basePluginId = $base_plugin_id;
56     $this->entityManager = $entity_manager;
57     $this->bundleInfoService = $bundle_info_service;
58   }
59
60   /**
61    * {@inheritdoc}
62    */
63   public static function create(ContainerInterface $container, $base_plugin_id) {
64     return new static(
65       $base_plugin_id,
66       $container->get('entity.manager'),
67       $container->get('entity_type.bundle.info')
68     );
69   }
70
71   /**
72    * {@inheritdoc}
73    */
74   public function getDerivativeDefinition($derivative_id, $base_plugin_definition) {
75     if (!empty($this->derivatives) && !empty($this->derivatives[$derivative_id])) {
76       return $this->derivatives[$derivative_id];
77     }
78     $this->getDerivativeDefinitions($base_plugin_definition);
79     if (isset($this->derivatives[$derivative_id])) {
80       return $this->derivatives[$derivative_id];
81     }
82   }
83
84   /**
85    * {@inheritdoc}
86    */
87   public function getDerivativeDefinitions($base_plugin_definition) {
88     // Also keep the 'entity' defined as is.
89     $this->derivatives[''] = $base_plugin_definition;
90     // Add definitions for each entity type and bundle.
91     foreach ($this->entityManager->getDefinitions() as $entity_type_id => $entity_type) {
92       $class = $entity_type->entityClassImplements(ConfigEntityInterface::class) ? ConfigEntityAdapter::class : EntityAdapter::class;
93       $this->derivatives[$entity_type_id] = [
94         'class' => $class,
95         'label' => $entity_type->getLabel(),
96         'constraints' => $entity_type->getConstraints(),
97         'internal' => $entity_type->isInternal(),
98       ] + $base_plugin_definition;
99
100       // Incorporate the bundles as entity:$entity_type:$bundle, if any.
101       foreach ($this->bundleInfoService->getBundleInfo($entity_type_id) as $bundle => $bundle_info) {
102         if ($bundle !== $entity_type_id) {
103           $this->derivatives[$entity_type_id . ':' . $bundle] = [
104             'class' => $class,
105             'label' => $bundle_info['label'],
106             'constraints' => $this->derivatives[$entity_type_id]['constraints'],
107           ] + $base_plugin_definition;
108         }
109       }
110     }
111     return $this->derivatives;
112   }
113
114 }