You are here

class EntityAliasTypeDeriver in Pathauto 8

Deriver that exposes content entities as alias type plugins.

Hierarchy

Expanded class hierarchy of EntityAliasTypeDeriver

File

src/Plugin/Deriver/EntityAliasTypeDeriver.php, line 19

Namespace

Drupal\pathauto\Plugin\Deriver
View source
class EntityAliasTypeDeriver extends DeriverBase implements ContainerDeriverInterface {
  use StringTranslationTrait;

  /**
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The entity field manager.
   *
   * @var \Drupal\Core\Entity\EntityFieldManagerInterface
   */
  protected $entityFieldManager;

  /**
   * @var \Drupal\token\TokenEntityMapperInterface
   */
  protected $tokenEntityMapper;

  /**
   * Constructs new EntityAliasTypeDeriver.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
   *   The entity field manager.
   * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
   *   The string translation service.
   * @param \Drupal\Token\TokenEntityMapperInterface $token_entity_mapper
   *   The token entity mapper.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, TranslationInterface $string_translation, TokenEntityMapperInterface $token_entity_mapper) {
    $this->entityTypeManager = $entity_type_manager;
    $this->entityFieldManager = $entity_field_manager;
    $this->stringTranslation = $string_translation;
    $this->tokenEntityMapper = $token_entity_mapper;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, $base_plugin_id) {
    return new static($container
      ->get('entity_type.manager'), $container
      ->get('entity_field.manager'), $container
      ->get('string_translation'), $container
      ->get('token.entity_mapper'));
  }

  /**
   * {@inheritdoc}
   */
  public function getDerivativeDefinitions($base_plugin_definition) {
    foreach ($this->entityTypeManager
      ->getDefinitions() as $entity_type_id => $entity_type) {

      // An entity type must have a canonical link template and support fields.
      if ($entity_type
        ->hasLinkTemplate('canonical') && is_subclass_of($entity_type
        ->getClass(), FieldableEntityInterface::class)) {
        $base_fields = $this->entityFieldManager
          ->getBaseFieldDefinitions($entity_type_id);
        if (!isset($base_fields['path'])) {

          // The entity type does not have a path field and is therefore not
          // supported.
          continue;
        }
        $this->derivatives[$entity_type_id] = $base_plugin_definition;
        $this->derivatives[$entity_type_id]['label'] = $entity_type
          ->getLabel();
        $this->derivatives[$entity_type_id]['types'] = [
          $this->tokenEntityMapper
            ->getTokenTypeForEntityType($entity_type_id),
        ];
        $this->derivatives[$entity_type_id]['provider'] = $entity_type
          ->getProvider();
        $this->derivatives[$entity_type_id]['context_definitions'] = [
          $entity_type_id => new EntityContextDefinition("entity:{$entity_type_id}", $this
            ->t('@label being aliased', [
            '@label' => $entity_type
              ->getLabel(),
          ])),
        ];
      }
    }
    return $this->derivatives;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DeriverBase::$derivatives protected property List of derivative definitions. 1
DeriverBase::getDerivativeDefinition public function Gets the definition of a derivative plugin. Overrides DeriverInterface::getDerivativeDefinition
EntityAliasTypeDeriver::$entityFieldManager protected property The entity field manager.
EntityAliasTypeDeriver::$entityTypeManager protected property
EntityAliasTypeDeriver::$tokenEntityMapper protected property
EntityAliasTypeDeriver::create public static function Creates a new class instance. Overrides ContainerDeriverInterface::create
EntityAliasTypeDeriver::getDerivativeDefinitions public function Gets the definition of all derivatives of a base plugin. Overrides DeriverBase::getDerivativeDefinitions
EntityAliasTypeDeriver::__construct public function Constructs new EntityAliasTypeDeriver.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.