View source
<?php
namespace Drupal\menu_condition\Plugin\Condition;
use Drupal\Core\Condition\ConditionInterface;
use Drupal\Core\Condition\ConditionPluginBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Menu\MenuActiveTrailInterface;
use Drupal\Core\Menu\MenuParentFormSelectorInterface;
use Drupal\Core\Menu\MenuLinkManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class MenuPosition extends ConditionPluginBase implements ConditionInterface, ContainerFactoryPluginInterface {
protected $menuActiveTrail;
protected $menuParentFormSelector;
protected $pluginManagerMenuLink;
public function __construct(array $configuration, $plugin_id, $plugin_definition, MenuActiveTrailInterface $menu_active_trail, MenuParentFormSelectorInterface $menu_parent_form_selector, MenuLinkManagerInterface $plugin_manager_menu_link) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->menuActiveTrail = $menu_active_trail;
$this->menuParentFormSelector = $menu_parent_form_selector;
$this->pluginManagerMenuLink = $plugin_manager_menu_link;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('menu.active_trail'), $container
->get('menu.parent_form_selector'), $container
->get('plugin.manager.menu.link'));
}
public function evaluate() {
if (empty($this->configuration['menu_parent'])) {
return TRUE;
}
list($menu_name, $link_plugin_id) = explode(':', $this->configuration['menu_parent'], 2);
$active_trail_ids = $this->menuActiveTrail
->getActiveTrailIds($menu_name);
if ($link_plugin_id) {
return isset($active_trail_ids[$link_plugin_id]);
}
else {
return (bool) array_filter($active_trail_ids);
}
}
public function summary() {
list($menu_name, $link_plugin_id) = explode(':', $this->configuration['menu_parent'], 2);
if ($link_plugin_id) {
$menu_link = $this->pluginManagerMenuLink
->createInstance($link_plugin_id);
return $this
->t('The menu item @link-title is either active or is in the active trail.', [
'@link-title' => $menu_link
->getTitle(),
]);
}
else {
return $this
->t('The active menu item is in the @menu-name menu.', [
'@menu-name' => $menu_name,
]);
}
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
if (!empty($this->configuration['menu_parent'])) {
$form['menu_parent'] = $this->menuParentFormSelector
->parentSelectElement($this->configuration['menu_parent']);
}
else {
$form['menu_parent'] = [
'#type' => 'select',
'#options' => $this->menuParentFormSelector
->getParentSelectOptions(),
];
}
$form['menu_parent']['#options'] = [
'' => $this
->t("- None -"),
] + $form['menu_parent']['#options'];
$form['menu_parent']['#title'] = $this
->t("Menu parent");
$form['menu_parent']['#description'] = $this
->t("Show the block on this menu item and all its children.");
return $form;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->configuration['menu_parent'] = $form_state
->getValue('menu_parent');
parent::submitConfigurationForm($form, $form_state);
}
public function defaultConfiguration() {
return [
'menu_parent' => '',
] + parent::defaultConfiguration();
}
}