You are here

public function AccordionMenusBlock::build in Accordion Menus 8

Same name and namespace in other branches
  1. 8.4 src/Plugin/Block/AccordionMenusBlock.php \Drupal\accordion_menus\Plugin\Block\AccordionMenusBlock::build()
  2. 8.2 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 23

Class

AccordionMenusBlock
Provides a accordion Menu block.

Namespace

Drupal\accordion_menus\Plugin\Block

Code

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;
}