View source
<?php
namespace Drupal\menu_block_current_language\Plugin\Block;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Menu\MenuActiveTrailInterface;
use Drupal\Core\Menu\MenuTreeParameters;
use Drupal\system\Plugin\Block\SystemMenuBlock;
use Symfony\Component\DependencyInjection\ContainerInterface;
class MenuBlockCurrentLanguage extends SystemMenuBlock {
protected $menuActiveTrail;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
$instance
->setMenuActiveTrail($container
->get('menu.active_trail'));
return $instance;
}
public function setMenuActiveTrail(MenuActiveTrailInterface $activeTrail) {
$this->menuActiveTrail = $activeTrail;
return $this;
}
public function build() {
$menu_name = $this
->getDerivativeId();
if ($this->configuration['expand_all_items']) {
$parameters = new MenuTreeParameters();
$active_trail = $this->menuActiveTrail
->getActiveTrailIds($menu_name);
$parameters
->setActiveTrail($active_trail);
}
else {
$parameters = $this->menuTree
->getCurrentRouteMenuTreeParameters($menu_name);
}
$level = $this->configuration['level'];
$depth = $this->configuration['depth'];
$parameters
->setMinDepth($level);
if ($depth > 0) {
$parameters
->setMaxDepth(min($level + $depth - 1, $this->menuTree
->maxDepth()));
}
if ($level > 1) {
if (count($parameters->activeTrail) >= $level) {
$menu_trail_ids = array_reverse(array_values($parameters->activeTrail));
$menu_root = $menu_trail_ids[$level - 1];
$parameters
->setRoot($menu_root)
->setMinDepth(1);
if ($depth > 0) {
$parameters
->setMaxDepth(min($level - 1 + $depth - 1, $this->menuTree
->maxDepth()));
}
}
else {
return [];
}
}
$tree = $this->menuTree
->load($menu_name, $parameters);
$manipulators = [
[
'callable' => 'menu.default_tree_manipulators:checkAccess',
],
[
'callable' => 'menu.default_tree_manipulators:generateIndexAndSort',
],
[
'callable' => 'menu_block_current_language_tree_manipulator::filterLanguages',
'args' => [
$this->configuration['translation_providers'],
],
],
];
$tree = $this->menuTree
->transform($tree, $manipulators);
return $this->menuTree
->build($tree);
}
public function blockSubmit($form, FormStateInterface $form_state) {
$this->configuration['translation_providers'] = $form_state
->getValue('translation_providers');
parent::blockSubmit($form, $form_state);
}
public function blockForm($form, FormStateInterface $form_state) {
$form = parent::blockForm($form, $form_state);
$form['translation_providers'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Enabled Core link types'),
'#options' => [
'menu_link_content' => $this
->t('Menu link content'),
'views' => $this
->t('Views'),
'default' => $this
->t('String translation (Experimental)'),
],
'#default_value' => $this->configuration['translation_providers'],
];
return $form;
}
public function defaultConfiguration() {
$config = [
'translation_providers' => [
'views' => 'views',
'menu_link_content' => 'menu_link_content',
'default' => 0,
],
];
return $config + parent::defaultConfiguration();
}
}