cf7b4a2fabc0e7f40c450bec0bef4df2b85bfafb
[yaffs-website] / web / modules / contrib / pathauto / src / Plugin / Deriver / EntityAliasTypeDeriver.php
1 <?php
2
3 namespace Drupal\pathauto\Plugin\Deriver;
4
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;
15
16 /**
17  * Deriver that exposes content entities as alias type plugins.
18  */
19 class EntityAliasTypeDeriver extends DeriverBase implements ContainerDeriverInterface {
20
21   use StringTranslationTrait;
22
23   /**
24    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
25    */
26   protected $entityTypeManager;
27
28   /**
29    * @var \Drupal\Core\Entity\EntityFieldManagerInterface
30    */
31   protected $entityFieldManager;
32
33   /**
34    * @var \Drupal\token\TokenEntityMapperInterface
35    */
36   protected $tokenEntityMapper;
37
38   /**
39    * Constructs new EntityAliasTypeDeriver.
40    *
41    * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
42    *   The entity type manager.
43    * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
44    *   The entity field manager.
45    * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
46    *   The string translation service.
47    * @apram \Drupal\Token\TokenEntityMapperInterface $token_entity_mapper
48    *   The token entity mapper.
49    */
50   public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, TranslationInterface $string_translation, TokenEntityMapperInterface $token_entity_mapper) {
51     $this->entityTypeManager = $entity_type_manager;
52     $this->entityFieldManager = $entity_field_manager;
53     $this->stringTranslation = $string_translation;
54     $this->tokenEntityMapper = $token_entity_mapper;
55   }
56
57   /**
58    * {@inheritdoc}
59    */
60   public static function create(ContainerInterface $container, $base_plugin_id) {
61     return new static(
62       $container->get('entity_type.manager'),
63       $container->get('entity_field.manager'),
64       $container->get('string_translation'),
65       $container->get('token.entity_mapper')
66     );
67   }
68
69   /**
70    * {@inheritdoc}
71    */
72   public function getDerivativeDefinitions($base_plugin_definition) {
73     foreach ($this->entityTypeManager->getDefinitions() as $entity_type_id => $entity_type) {
74       // An entity type must have a canonical link template and support fields.
75       if ($entity_type->hasLinkTemplate('canonical') && is_subclass_of($entity_type->getClass(), FieldableEntityInterface::class)) {
76         $base_fields = $this->entityFieldManager->getBaseFieldDefinitions($entity_type_id);
77         if (!isset($base_fields['path'])) {
78           // The entity type does not have a path field and is therefore not
79           // supported.
80           continue;
81         }
82         $this->derivatives[$entity_type_id] = $base_plugin_definition;
83         $this->derivatives[$entity_type_id]['label'] = $entity_type->getLabel();
84         $this->derivatives[$entity_type_id]['types'] = [$this->tokenEntityMapper->getTokenTypeForEntityType($entity_type_id)];
85         $this->derivatives[$entity_type_id]['provider'] = $entity_type->getProvider();
86         $this->derivatives[$entity_type_id]['context'] = [
87           $entity_type_id => new ContextDefinition("entity:$entity_type_id", $this->t('@label being aliased', ['@label' => $entity_type->getLabel()]))
88         ];
89       }
90     }
91     return $this->derivatives;
92   }
93
94 }