You are here

public function MenuBlockCurrentLanguage::build in Menu block current language 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/MenuBlockCurrentLanguage.php, line 59

Class

MenuBlockCurrentLanguage
Provides a generic Menu block.

Namespace

Drupal\menu_block_current_language\Plugin\Block

Code

public function build() {
  $menu_name = $this
    ->getDerivativeId();
  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()));
  }

  // For menu blocks with start level greater than 1, only show menu items
  // from the current active trail. Adjust the root according to the current
  // position in the menu in order to determine if we can show the subtree.
  if ($level > 1) {
    if (count($parameters->activeTrail) >= $level) {

      // Active trail array is child-first. Reverse it, and pull the new menu
      // root based on the parent of the configured start level.
      $menu_trail_ids = array_reverse(array_values($parameters->activeTrail));
      $menu_root = $menu_trail_ids[$level - 1];
      $parameters
        ->setRoot($menu_root)
        ->setMinDepth(1);
      if ($depth > 0) {
        $parameters
          ->setMaxDepth(min($level - 1 + $depth - 1, $this->menuTree
          ->maxDepth()));
      }
    }
    else {
      return [];
    }
  }
  $tree = $this->menuTree
    ->load($menu_name, $parameters);
  $manipulators = [
    [
      'callable' => 'menu.default_tree_manipulators:checkAccess',
    ],
    [
      'callable' => 'menu.default_tree_manipulators:generateIndexAndSort',
    ],
    [
      'callable' => 'menu_block_current_language_tree_manipulator::filterLanguages',
      'args' => [
        $this->configuration['translation_providers'],
      ],
    ],
  ];
  $tree = $this->menuTree
    ->transform($tree, $manipulators);
  return $this->menuTree
    ->build($tree);
}