Updated all the contrib modules to their latest versions.
[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    * The entity field manager.
30    *
31    * @var \Drupal\Core\Entity\EntityFieldManagerInterface
32    */
33   protected $entityFieldManager;
34
35   /**
36    * @var \Drupal\token\TokenEntityMapperInterface
37    */
38   protected $tokenEntityMapper;
39
40   /**
41    * Constructs new EntityAliasTypeDeriver.
42    *
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.
51    */
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;
57   }
58
59   /**
60    * {@inheritdoc}
61    */
62   public static function create(ContainerInterface $container, $base_plugin_id) {
63     return new static(
64       $container->get('entity_type.manager'),
65       $container->get('entity_field.manager'),
66       $container->get('string_translation'),
67       $container->get('token.entity_mapper')
68     );
69   }
70
71   /**
72    * {@inheritdoc}
73    */
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
81           // supported.
82           continue;
83         }
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()]))
90         ];
91       }
92     }
93     return $this->derivatives;
94   }
95
96 }