3 namespace Drupal\Core\Entity\Plugin\DataType\Deriver;
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;
14 * Provides data type plugins for each existing entity type and bundle.
16 class EntityDeriver implements ContainerDeriverInterface {
19 * List of derivative definitions.
23 protected $derivatives = [];
26 * The base plugin ID this derivative is for.
30 protected $basePluginId;
35 * @var \Drupal\Core\Entity\EntityManagerInterface
37 protected $entityManager;
40 * The bundle info service.
42 * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
44 protected $bundleInfoService;
47 * Constructs an EntityDeriver object.
49 * @param string $base_plugin_id
51 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
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;
63 public static function create(ContainerInterface $container, $base_plugin_id) {
66 $container->get('entity.manager'),
67 $container->get('entity_type.bundle.info')
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];
78 $this->getDerivativeDefinitions($base_plugin_definition);
79 if (isset($this->derivatives[$derivative_id])) {
80 return $this->derivatives[$derivative_id];
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] = [
95 'label' => $entity_type->getLabel(),
96 'constraints' => $entity_type->getConstraints(),
97 'internal' => $entity_type->isInternal(),
98 ] + $base_plugin_definition;
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] = [
105 'label' => $bundle_info['label'],
106 'constraints' => $this->derivatives[$entity_type_id]['constraints'],
107 ] + $base_plugin_definition;
111 return $this->derivatives;