You are here

public function AccordionMenusBlock::build in Accordion Menus 8.2

Same name and namespace in other branches
  1. 8.4 src/Plugin/Block/AccordionMenusBlock.php \Drupal\accordion_menus\Plugin\Block\AccordionMenusBlock::build()
  2. 8 src/Plugin/Block/AccordionMenusBlock.php \Drupal\accordion_menus\Plugin\Block\AccordionMenusBlock::build()
  3. 8.3 src/Plugin/Block/AccordionMenusBlock.php \Drupal\accordion_menus\Plugin\Block\AccordionMenusBlock::build()

Builds and returns the renderable array for this block plugin.

If a block should not be rendered because it has no content, then this method must also ensure to return no content: it must then only return an empty array, or an empty array with #cache set (with cacheability metadata indicating the circumstances for it being empty).

Return value

array A renderable array representing the content of the block.

Overrides BlockPluginInterface::build

See also

\Drupal\block\BlockViewBuilder

File

src/Plugin/Block/AccordionMenusBlock.php, line 62

Class

AccordionMenusBlock
Provides a accordion Menu block.

Namespace

Drupal\accordion_menus\Plugin\Block

Code

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',
      ],
    ],
  ];
}