diff --git a/src/Menu/EntityAddLocalAction.php b/src/Menu/EntityAddLocalAction.php index b39018a..8b7a04e 100644 --- a/src/Menu/EntityAddLocalAction.php +++ b/src/Menu/EntityAddLocalAction.php @@ -2,7 +2,7 @@ namespace Drupal\entity\Menu; -use Drupal\Core\Entity\EntityTypeManagerInterface; +use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Menu\LocalActionDefault; use Drupal\Core\Routing\RouteProviderInterface; use Drupal\Core\StringTranslation\StringTranslationTrait; @@ -18,11 +18,11 @@ class EntityAddLocalAction extends LocalActionDefault { use StringTranslationTrait; /** - * The entity type manager. + * The entity type. * - * @var \Drupal\Core\Entity\EntityTypeManagerInterface + * @var \Drupal\Core\Entity\EntityTypeInterface */ - protected $entityTypeManager; + protected $entityType; /** * Constructs a EntityAddLocalAction object. @@ -35,15 +35,15 @@ class EntityAddLocalAction extends LocalActionDefault { * The plugin implementation definition. * @param \Drupal\Core\Routing\RouteProviderInterface $route_provider * The route provider to load routes by name. - * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager - * The entity type manager. + * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type + * The entity type. * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation * The string translation service. */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, RouteProviderInterface $route_provider, EntityTypeManagerInterface $entity_type_manager, TranslationInterface $string_translation) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, RouteProviderInterface $route_provider, EntityTypeInterface $entity_type, TranslationInterface $string_translation) { parent::__construct($configuration, $plugin_id, $plugin_definition, $route_provider); - $this->entityTypeManager = $entity_type_manager; + $this->entityType = $entity_type; $this->setStringTranslation($string_translation); } @@ -51,12 +51,20 @@ class EntityAddLocalAction extends LocalActionDefault { * {@inheritdoc} */ public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + /* @var \Drupal\Core\Entity\EntityTypeManagerInterface */ + $entity_type_manager = $container->get('entity_type.manager'); + // The plugin ID is of the form + // "entity.entity_actions:entity.$entity_type_id.collection". + // @see entity.links.action.yml + // @see \Drupal\entity\Menu\EntityCollectionLocalActionProvider::buildLocalActions() + list(, $derivate_id) = explode(':', $plugin_id); + list(, $entity_type_id, ) = explode('.', $derivate_id); return new static( $configuration, $plugin_id, $plugin_definition, $container->get('router.route_provider'), - $container->get('entity_type.manager'), + $entity_type_manager->getDefinition($entity_type_id), $container->get('string_translation') ); } @@ -65,16 +73,9 @@ class EntityAddLocalAction extends LocalActionDefault { * {@inheritdoc} */ public function getTitle(Request $request = NULL) { - $route_name = $this->getRouteName(); - $entity_types = $this->entityTypeManager->getDefinitions(); - foreach ($entity_types as $entity_type_id => $entity_type) { - if (($route_name === "entity.$entity_type_id.add_page") || ($route_name === "entity.$entity_type_id.add_form")) { - return (string) $this->t('Add @entity', [ - '@entity' => (string) $entity_type->getSingularLabel(), - ]); - } - } - return parent::getTitle($request); + return (string) $this->t('Add @entity', [ + '@entity' => (string) $this->entityType->getSingularLabel(), + ]); } }