You are here

public function AccordionMenusBlock::build in Accordion Menus 8.3

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.2 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 92

Class

AccordionMenusBlock
Provides a accordion Menu block.

Namespace

Drupal\accordion_menus\Plugin\Block

Code

public function build() {
  $items = [];
  $menu_name = $this
    ->getDerivativeId();
  $parameters = $this->menuTree
    ->getCurrentRouteMenuTreeParameters($menu_name);
  $parameters
    ->setMinDepth(0)
    ->onlyEnabledLinks();
  $manipulators = [
    [
      'callable' => 'menu.default_tree_manipulators:checkAccess',
    ],
    [
      'callable' => 'menu.default_tree_manipulators:generateIndexAndSort',
    ],
  ];
  $tree = $this->menuTree
    ->load($menu_name, $parameters);
  $tree = $this->menuTree
    ->transform($tree, $manipulators);

  // Get accordion configuration.
  $config = $this->configFactory
    ->getEditable('accordion_menus.settings');
  $closed_by_default = array_filter($config
    ->get('accordion_menus_default_closed'));
  $no_submenu = $config
    ->get('accordion_menus_no_submenus');
  $without_submenu = in_array($menu_name, $no_submenu, TRUE) ? TRUE : FALSE;
  foreach ($tree as $key => $item) {
    $link = $item->link;

    // Only render accessible links.
    if ($this
      ->isAccordionMenusLinkInaccessible($item)) {
      continue;
    }
    if ($item->subtree) {
      $items[$key] = [
        'content' => $this
          ->generateSubMenuTree($item->subtree),
        'title' => $link
          ->getTitle(),
      ];
    }
    elseif ($without_submenu) {
      $items[$key] = [
        'content' => [
          '#theme' => 'item_list',
          '#items' => [
            Link::fromTextAndUrl($link
              ->getTitle(), $link
              ->getUrlObject()),
          ],
        ],
        'title' => $link
          ->getTitle(),
      ];
    }
  }
  return [
    '#theme' => 'accordian_menus_block',
    '#elements' => [
      'menu_name' => $menu_name,
      'items' => $items,
    ],
    '#attached' => [
      'library' => [
        'accordion_menus/accordion_menus_widget',
      ],
      'drupalSettings' => [
        'accordion_menus' => [
          'accordion_closed' => $closed_by_default,
        ],
      ],
    ],
  ];
}