You are here

public function DomainMenuAccessMenuBlock::build in Domain Menu Access 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 SystemMenuBlock::build

See also

\Drupal\block\BlockViewBuilder

File

src/Plugin/Block/DomainMenuAccessMenuBlock.php, line 25

Class

DomainMenuAccessMenuBlock
Provides a Domain Access Menu block.

Namespace

Drupal\domain_menu_access\Plugin\Block

Code

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

  // Fallback on default menu if not restricted by domain.
  if (!$this
    ->isDomainRestricted($menu_name)) {
    return parent::build();
  }
  $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 = [
    [
      'callable' => 'menu.default_tree_manipulators:checkAccess',
    ],
    [
      'callable' => 'domain_menu_access.default_tree_manipulators:checkDomain',
    ],
    [
      'callable' => 'menu.default_tree_manipulators:generateIndexAndSort',
    ],
  ];
  $tree = $this->menuTree
    ->transform($tree, $manipulators);
  return $this->menuTree
    ->build($tree);
}