3 namespace Drupal\Core\Entity\Plugin\DataType\Deriver;
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;
11 * Provides data type plugins for each existing entity type and bundle.
13 class EntityDeriver implements ContainerDeriverInterface {
16 * List of derivative definitions.
20 protected $derivatives = [];
23 * The base plugin ID this derivative is for.
27 protected $basePluginId;
32 * @var \Drupal\Core\Entity\EntityManagerInterface
34 protected $entityManager;
37 * The bundle info service.
39 * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
41 protected $bundleInfoService;
44 * Constructs an EntityDeriver object.
46 * @param string $base_plugin_id
48 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
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;
60 public static function create(ContainerInterface $container, $base_plugin_id) {
63 $container->get('entity.manager'),
64 $container->get('entity_type.bundle.info')
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];
75 $this->getDerivativeDefinitions($base_plugin_definition);
76 if (isset($this->derivatives[$derivative_id])) {
77 return $this->derivatives[$derivative_id];
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;
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;
105 return $this->derivatives;