You are here

public function SmartMenusBlock::build in Smartmenus.js 8

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/SmartMenusBlock.php, line 213

Class

SmartMenusBlock
Provides a 'SmartMenusBlock' block.

Namespace

Drupal\smartmenus\Plugin\Block

Code

public function build() {
  $menu_name = $this
    ->getSelectedMenu();

  // Get the menu parameters for the current route, but remove the expanded
  // parents, so that we get the entire tree.
  $parameters = $this->menuLinkTree
    ->getCurrentRouteMenuTreeParameters($menu_name);
  $parameters->expandedParents = [];

  // Get the selected menu.
  $tree = $this->menuLinkTree
    ->load($menu_name, $parameters);
  $manipulators = [
    [
      'callable' => 'menu.default_tree_manipulators:checkAccess',
    ],
    [
      'callable' => 'menu.default_tree_manipulators:generateIndexAndSort',
    ],
  ];

  // Run transform to check the access and sort the menu items
  $tree = $this->menuLinkTree
    ->transform($tree, $manipulators);
  $tree = $this->menuLinkTree
    ->build($tree);
  $build['#theme'] = 'smartmenus_block';
  $build['#attached']['library'] = [
    'smartmenus/smartmenus-custom',
  ];
  $menuOrientation = 'sm-' . $this
    ->getSelectedOrientation();
  $toggle = null;
  if ($this
    ->isToggleEnabled()) {
    $render_toggle = [
      '#theme' => 'smartmenus_toggle',
    ];
    $toggle = $this->renderer
      ->renderPlain($render_toggle);
  }
  $rendered_menu = [
    '#theme' => 'smartmenus_menu',
    '#items' => $tree['#items'],
    '#attributes' => [
      'class' => [
        $this
          ->getSelectedTheme(),
        $menuOrientation,
      ],
    ],
    '#menu_name' => $menu_name,
    '#toggle' => $toggle,
    '#cache' => [
      'contexts' => [
        'url.path',
      ],
    ],
  ];
  $build['#menu_tree'] = $this->renderer
    ->renderPlain($rendered_menu);
  return $build;
}