EntityAliasTypeDeriver.php in View Mode Page 4.0.x
File
src/Plugin/Derivative/EntityAliasTypeDeriver.php
View source
<?php
namespace Drupal\view_mode_page\Plugin\Derivative;
use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Plugin\Context\EntityContextDefinition;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\token\TokenEntityMapperInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class EntityAliasTypeDeriver extends DeriverBase implements ContainerDeriverInterface {
use StringTranslationTrait;
protected $entityTypeManager;
protected $entityFieldManager;
protected $tokenEntityMapper;
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;
}
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'));
}
public function getDerivativeDefinitions($base_plugin_definition) {
foreach ($this->entityTypeManager
->getDefinitions() as $entity_type_id => $entity_type) {
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'])) {
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;
}
}