View source
<?php
namespace Drupal\menu_item_extras\Plugin\views\argument_default;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Menu\MenuLinkTreeInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\views\Plugin\views\argument_default\ArgumentDefaultPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
class MenuLinkContentId extends ArgumentDefaultPluginBase implements CacheableDependencyInterface {
protected $routeMatch;
protected $entityTypeManager;
protected $menuTree;
protected $currentRequest;
public function __construct(array $configuration, $plugin_id, $plugin_definition, RouteMatchInterface $route_match, EntityTypeManagerInterface $entity_type_manager, MenuLinkTreeInterface $menu_tree, Request $request) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->routeMatch = $route_match;
$this->entityTypeManager = $entity_type_manager;
$this->menuTree = $menu_tree;
$this->currentRequest = $request;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('current_route_match'), $container
->get('entity_type.manager'), $container
->get('menu.link_tree'), $container
->get('request_stack')
->getCurrentRequest());
}
protected function defineOptions() {
$options = parent::defineOptions();
$options['menu'] = [
'default' => 'main',
];
return $options;
}
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
$options = [];
$menus = $this
->getMenus();
foreach ($menus as $menu) {
$options[$menu
->id()] = $menu
->label();
}
$default = $this->options['menu'];
if (!isset($options[$default])) {
$default = isset($options['main']) ? 'main' : '';
}
$form['menu'] = [
'#type' => 'select',
'#title' => $this
->t('Menu'),
'#options' => $options,
'#default_value' => $default,
];
}
public function getArgument() {
if ($this->options['menu']) {
$current_path = $this->currentRequest
->getRequestUri();
$parameters = $this->menuTree
->getCurrentRouteMenuTreeParameters($this->options['menu']);
$parameters
->onlyEnabledLinks();
$menu_level = $this->menuTree
->load($this->options['menu'], $parameters);
$active_item = $this
->getActiveMenuItem($menu_level, $current_path);
if ($active_item) {
$link = $active_item->link;
$metadata = $link
->getMetaData();
return $metadata['entity_id'];
}
}
}
public function getActiveMenuItem(array $menu_level, $current_path) {
$active_item = NULL;
foreach ($menu_level as $menu_item) {
if ($menu_item->inActiveTrail) {
$link = $menu_item->link;
$url_object = $link
->getUrlObject();
$item_path = $url_object
->toString();
if ($item_path === $current_path) {
$active_item = $menu_item;
}
if ($menu_item->subtree && !$active_item) {
$active_item = $this
->getActiveMenuItem($menu_item->subtree, $current_path);
}
}
}
return $active_item;
}
public function getCacheMaxAge() {
return Cache::PERMANENT;
}
public function getCacheContexts() {
return [
'url',
];
}
public function calculateDependencies() {
$dependencies = parent::calculateDependencies();
return $dependencies;
}
public function getMenus() {
$menus = $this->entityTypeManager
->getStorage('menu')
->loadMultiple();
return $menus;
}
}