3 namespace Drupal\pathauto\Plugin\Deriver;
5 use Drupal\Component\Plugin\Derivative\DeriverBase;
6 use Drupal\Core\Entity\EntityFieldManagerInterface;
7 use Drupal\Core\Entity\EntityTypeManagerInterface;
8 use Drupal\Core\Entity\FieldableEntityInterface;
9 use Drupal\Core\Plugin\Context\ContextDefinition;
10 use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
11 use Drupal\Core\StringTranslation\StringTranslationTrait;
12 use Drupal\Core\StringTranslation\TranslationInterface;
13 use Drupal\token\TokenEntityMapperInterface;
14 use Symfony\Component\DependencyInjection\ContainerInterface;
17 * Deriver that exposes content entities as alias type plugins.
19 class EntityAliasTypeDeriver extends DeriverBase implements ContainerDeriverInterface {
21 use StringTranslationTrait;
24 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
26 protected $entityTypeManager;
29 * The entity field manager.
31 * @var \Drupal\Core\Entity\EntityFieldManagerInterface
33 protected $entityFieldManager;
36 * @var \Drupal\token\TokenEntityMapperInterface
38 protected $tokenEntityMapper;
41 * Constructs new EntityAliasTypeDeriver.
43 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
44 * The entity type manager.
45 * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
46 * The entity field manager.
47 * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
48 * The string translation service.
49 * @param \Drupal\Token\TokenEntityMapperInterface $token_entity_mapper
50 * The token entity mapper.
52 public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, TranslationInterface $string_translation, TokenEntityMapperInterface $token_entity_mapper) {
53 $this->entityTypeManager = $entity_type_manager;
54 $this->entityFieldManager = $entity_field_manager;
55 $this->stringTranslation = $string_translation;
56 $this->tokenEntityMapper = $token_entity_mapper;
62 public static function create(ContainerInterface $container, $base_plugin_id) {
64 $container->get('entity_type.manager'),
65 $container->get('entity_field.manager'),
66 $container->get('string_translation'),
67 $container->get('token.entity_mapper')
74 public function getDerivativeDefinitions($base_plugin_definition) {
75 foreach ($this->entityTypeManager->getDefinitions() as $entity_type_id => $entity_type) {
76 // An entity type must have a canonical link template and support fields.
77 if ($entity_type->hasLinkTemplate('canonical') && is_subclass_of($entity_type->getClass(), FieldableEntityInterface::class)) {
78 $base_fields = $this->entityFieldManager->getBaseFieldDefinitions($entity_type_id);
79 if (!isset($base_fields['path'])) {
80 // The entity type does not have a path field and is therefore not
84 $this->derivatives[$entity_type_id] = $base_plugin_definition;
85 $this->derivatives[$entity_type_id]['label'] = $entity_type->getLabel();
86 $this->derivatives[$entity_type_id]['types'] = [$this->tokenEntityMapper->getTokenTypeForEntityType($entity_type_id)];
87 $this->derivatives[$entity_type_id]['provider'] = $entity_type->getProvider();
88 $this->derivatives[$entity_type_id]['context'] = [
89 $entity_type_id => new ContextDefinition("entity:$entity_type_id", $this->t('@label being aliased', ['@label' => $entity_type->getLabel()]))
93 return $this->derivatives;