View source
<?php
namespace Drupal\accordion_menus\Plugin\Block;
use Drupal\Core\Link;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Menu\MenuLinkTreeInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class AccordionMenusBlock extends BlockBase implements ContainerFactoryPluginInterface {
protected $menuTree;
public function __construct(array $configuration, $plugin_id, array $plugin_definition, MenuLinkTreeInterface $menu_tree) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->menuTree = $menu_tree;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('menu.link_tree'));
}
public function build() {
$elements = [];
$menu_name = $this
->getDerivativeId();
$parameters = $this->menuTree
->getCurrentRouteMenuTreeParameters($menu_name);
$parameters
->setMinDepth(0)
->onlyEnabledLinks();
$tree = $this->menuTree
->load($menu_name, $parameters);
$manipulators = [
[
'callable' => 'menu.default_tree_manipulators:checkAccess',
],
[
'callable' => 'menu.default_tree_manipulators:generateIndexAndSort',
],
];
$tree = $this->menuTree
->transform($tree, $manipulators);
foreach ($tree as $key => $menu_item) {
if ($menu_item->hasChildren) {
$elements[$key] = [
'content' => $this
->generateSubMenuTree($menu_item->subtree),
'title' => $menu_item->link
->getTitle(),
];
}
}
return [
'#theme' => 'accordian_menus_block',
'#elements' => $elements,
'#attached' => [
'library' => [
'accordion_menus/accordion_menus_widget',
],
],
];
}
public function generateSubMenuTree($sub_menus) {
$items = [];
foreach ($sub_menus as $sub_menu) {
if ($sub_menu->link
->isEnabled()) {
$items[] = Link::fromTextAndUrl($sub_menu->link
->getTitle(), $sub_menu->link
->getUrlObject());
}
}
return [
'#theme' => 'item_list',
'#items' => $items,
];
}
}