AccordionMenusBlock.php in Accordion Menus 8
File
src/Plugin/Block/AccordionMenusBlock.php
View source
<?php
namespace Drupal\accordion_menus\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Link;
class AccordionMenusBlock extends BlockBase {
public function build() {
$elements = [];
$output = [];
$menu_name = $this
->getDerivativeId();
$menu_tree = \Drupal::menuTree();
$parameters = $menu_tree
->getCurrentRouteMenuTreeParameters($menu_name);
$parameters
->setMinDepth(0)
->onlyEnabledLinks();
$tree = $menu_tree
->load($menu_name, $parameters);
$manipulators = array(
array(
'callable' => 'menu.default_tree_manipulators:checkAccess',
),
array(
'callable' => 'menu.default_tree_manipulators:generateIndexAndSort',
),
);
$tree = $menu_tree
->transform($tree, $manipulators);
$output['#theme'] = 'accordian_menus_block';
$output['#attached']['library'][] = 'accordion_menus/accordion_menus_widget';
foreach ($tree as $key => $menu_item) {
if ($menu_item->hasChildren) {
$elements[$key] = [
'content' => $this
->generateSubMenuTree($menu_item->subtree),
'title' => $menu_item->link
->getTitle(),
];
}
}
$output['#elements'] = $elements;
return $output;
}
private function generateSubMenuTree($menu) {
$output = [];
$item_lists = [];
foreach ($menu as $item) {
if ($item->link
->isEnabled()) {
$item_lists[] = Link::fromTextAndUrl($item->link
->getTitle(), $item->link
->getUrlObject());
}
}
$output = [
'#theme' => 'item_list',
'#items' => $item_lists,
];
return $output;
}
}