You are here

public function GroupMenuBlock::build in Group Content Menu 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/GroupMenuBlock.php, line 166

Class

GroupMenuBlock
Provides a generic Menu block.

Namespace

Drupal\group_content_menu\Plugin\Block

Code

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

  // If unable to determine the menu, prevent the block from rendering.
  if (!($menu_name = $this
    ->getMenuName())) {
    return [];
  }
  if ($this->configuration['expand_all_items']) {
    $parameters = new MenuTreeParameters();
    $active_trail = $this->menuActiveTrail
      ->getActiveTrailIds($menu_name);
    $parameters
      ->setActiveTrail($active_trail);
  }
  else {
    $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' => 'menu.default_tree_manipulators:generateIndexAndSort',
    ],
  ];
  $tree = $this->menuTree
    ->transform($tree, $manipulators);
  $build = $this->menuTree
    ->build($tree);
  $menu_instance = $this
    ->getMenuInstance();
  if ($menu_instance instanceof GroupContentMenuInterface) {
    $build['#contextual_links']['group_menu'] = [
      'route_parameters' => [
        'group' => $this
          ->getContext('group')
          ->getContextData()
          ->getValue()
          ->id(),
        'group_content_menu' => $menu_instance
          ->id(),
      ],
    ];
  }
  if ($menu_instance) {
    $build['#theme'] = 'menu__group_menu';
  }
  return $build;
}