You are here

public function SystemMenuBlock::build in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/src/Plugin/Block/SystemMenuBlock.php \Drupal\system\Plugin\Block\SystemMenuBlock::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

core/modules/system/src/Plugin/Block/SystemMenuBlock.php, line 139
Contains \Drupal\system\Plugin\Block\SystemMenuBlock.

Class

SystemMenuBlock
Provides a generic Menu block.

Namespace

Drupal\system\Plugin\Block

Code

public function build() {
  $menu_name = $this
    ->getDerivativeId();
  $parameters = $this->menuTree
    ->getCurrentRouteMenuTreeParameters($menu_name);

  // Adjust the menu tree parameters based on the block's configuration.
  $level = $this->configuration['level'];
  $depth = $this->configuration['depth'];
  $parameters
    ->setMinDepth($level);

  // When the depth is configured to zero, there is no depth limit. When depth
  // is non-zero, it indicates the number of levels that must be displayed.
  // Hence this is a relative depth that we must convert to an actual
  // (absolute) depth, that may never exceed the maximum depth.
  if ($depth > 0) {
    $parameters
      ->setMaxDepth(min($level + $depth - 1, $this->menuTree
      ->maxDepth()));
  }
  $tree = $this->menuTree
    ->load($menu_name, $parameters);
  $manipulators = array(
    array(
      'callable' => 'menu.default_tree_manipulators:checkAccess',
    ),
    array(
      'callable' => 'menu.default_tree_manipulators:generateIndexAndSort',
    ),
  );
  $tree = $this->menuTree
    ->transform($tree, $manipulators);
  return $this->menuTree
    ->build($tree);
}