EntityDeriver.php in Drupal 10
File
core/lib/Drupal/Core/Entity/Plugin/DataType/Deriver/EntityDeriver.php
View source
<?php
namespace Drupal\Core\Entity\Plugin\DataType\Deriver;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\Plugin\DataType\ConfigEntityAdapter;
use Drupal\Core\Entity\Plugin\DataType\EntityAdapter;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class EntityDeriver implements ContainerDeriverInterface {
protected $derivatives = [];
protected $basePluginId;
protected $entityTypeManager;
protected $bundleInfoService;
public function __construct($base_plugin_id, EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $bundle_info_service) {
$this->basePluginId = $base_plugin_id;
$this->entityTypeManager = $entity_type_manager;
$this->bundleInfoService = $bundle_info_service;
}
public static function create(ContainerInterface $container, $base_plugin_id) {
return new static($base_plugin_id, $container
->get('entity_type.manager'), $container
->get('entity_type.bundle.info'));
}
public function getDerivativeDefinition($derivative_id, $base_plugin_definition) {
if (!empty($this->derivatives) && !empty($this->derivatives[$derivative_id])) {
return $this->derivatives[$derivative_id];
}
$this
->getDerivativeDefinitions($base_plugin_definition);
if (isset($this->derivatives[$derivative_id])) {
return $this->derivatives[$derivative_id];
}
}
public function getDerivativeDefinitions($base_plugin_definition) {
$this->derivatives[''] = $base_plugin_definition;
foreach ($this->entityTypeManager
->getDefinitions() as $entity_type_id => $entity_type) {
$class = $entity_type
->entityClassImplements(ConfigEntityInterface::class) ? ConfigEntityAdapter::class : EntityAdapter::class;
$this->derivatives[$entity_type_id] = [
'class' => $class,
'label' => $entity_type
->getLabel(),
'constraints' => $entity_type
->getConstraints(),
'internal' => $entity_type
->isInternal(),
] + $base_plugin_definition;
$bundle_info = $this->bundleInfoService
->getBundleInfo($entity_type_id);
if (count($bundle_info) > 1 || $entity_type
->getKey('bundle')) {
foreach ($bundle_info as $bundle => $info) {
$this->derivatives[$entity_type_id . ':' . $bundle] = [
'class' => $class,
'label' => $info['label'],
'constraints' => $this->derivatives[$entity_type_id]['constraints'],
] + $base_plugin_definition;
}
}
}
return $this->derivatives;
}
}
Classes
Name |
Description |
EntityDeriver |
Provides data type plugins for each existing entity type and bundle. |