View source
<?php
namespace Drupal\menu_position\Plugin\Menu;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Controller\TitleResolverInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Menu\MenuLinkBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Routing\AdminContext;
use Drupal\Core\Routing\RouteMatchInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class MenuPositionLink extends MenuLinkBase implements ContainerFactoryPluginInterface {
protected $adminContext;
protected $entityTypeManager;
protected $renderer;
protected $requestStack;
protected $routeMatch;
protected $titleResolver;
protected $settings;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, ConfigFactoryInterface $config_factory, RouteMatchInterface $route_match, RequestStack $request_stack, AdminContext $admin_context, TitleResolverInterface $title_resolver, RendererInterface $renderer) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entity_type_manager;
$this->settings = $config_factory
->get('menu_position.settings');
$this->routeMatch = $route_match;
$this->requestStack = $request_stack;
$this->adminContext = $admin_context;
$this->titleResolver = $title_resolver;
$this->renderer = $renderer;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager'), $container
->get('config.factory'), $container
->get('current_route_match'), $container
->get('request_stack'), $container
->get('router.admin_context'), $container
->get('title_resolver'), $container
->get('renderer'));
}
protected $overrideAllowed = [
'parent' => 1,
'weight' => 1,
];
public function getTitle() {
if ($this->adminContext
->isAdminRoute()) {
return $this->pluginDefinition['title'];
}
else {
$title = $this->titleResolver
->getTitle($this->requestStack
->getCurrentRequest(), $this->routeMatch
->getRouteObject());
if (is_array($title)) {
$title = $this->renderer
->renderPlain($title);
}
return $title;
}
}
public function getDescription() {
return $this
->getPluginDefinition()['description'];
}
public function updateLink(array $new_definition_values, $persist) {
$overrides = array_intersect_key($new_definition_values, $this->overrideAllowed);
$this->pluginDefinition = $overrides + $this
->getPluginDefinition();
return $this->pluginDefinition;
}
public function isDeletable() {
return TRUE;
}
public function isEnabled() {
return (bool) ($this->settings
->get('link_display') === 'child');
}
public function deleteLink() {
}
public function getEditRoute() {
$storage = $this->entityTypeManager
->getStorage('menu_position_rule');
$entity_id = $this->pluginDefinition['metadata']['entity_id'];
$entity = $storage
->load($entity_id);
return $entity
->toUrl();
}
}