MenuLinkEntity.php in Admin Toolbar 8.2
File
admin_toolbar_tools/src/Plugin/Menu/MenuLinkEntity.php
View source
<?php
namespace Drupal\admin_toolbar_tools\Plugin\Menu;
use Drupal\Core\Entity\EntityDescriptionInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Menu\MenuLinkDefault;
use Drupal\Core\Menu\StaticMenuLinkOverridesInterface;
use Drupal\node\NodeTypeInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class MenuLinkEntity extends MenuLinkDefault {
protected $entity;
public function __construct(array $configuration, $plugin_id, $plugin_definition, StaticMenuLinkOverridesInterface $static_override, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $static_override);
$this->entity = $entity_type_manager
->getStorage($this->pluginDefinition['metadata']['entity_type'])
->load($this->pluginDefinition['metadata']['entity_id']);
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('menu_link.static.overrides'), $container
->get('entity_type.manager'));
}
public function getTitle() {
if ($this->entity) {
return (string) $this->entity
->label();
}
return $this->pluginDefinition['title'] ?: $this
->t('Missing');
}
public function getDescription() {
if ($this->entity instanceof EntityDescriptionInterface || $this->entity instanceof NodeTypeInterface) {
return $this->entity
->getDescription();
}
return parent::getDescription();
}
public function getCacheContexts() {
if ($this->entity) {
return $this->entity
->getCacheContexts();
}
return parent::getCacheContexts();
}
public function getCacheTags() {
if ($this->entity) {
return $this->entity
->getCacheTags();
}
return parent::getCacheTags();
}
public function getCacheMaxAge() {
if ($this->entity) {
return $this->entity
->getCacheMaxAge();
}
return parent::getCacheMaxAge();
}
}