You are here

public function EntitySubmenuBlock::build in Entity Submenu Block 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/EntitySubmenuBlock.php, line 157

Class

EntitySubmenuBlock
Provides an Entity Submenu Block.

Namespace

Drupal\entity_submenu_block\Plugin\Block

Code

public function build() {
  $build = [
    '#theme' => 'entity_submenu',
    '#menu_name' => NULL,
    '#menu_items' => [],
  ];

  // Get the menu name.
  $menu_name = $this
    ->getDerivativeId();

  // Return empty menu items array if the active trail is not in this menu.
  if (empty($this->menuActiveTrail
    ->getActiveLink($menu_name))) {
    return $build;
  }

  // The menu name is only set if the active trail is in this menu.
  $build['#menu_name'] = $menu_name;
  $parameters = $this->menuTree
    ->getCurrentRouteMenuTreeParameters($menu_name);

  // Get current level from end of active trail.
  $level = count($parameters->activeTrail);
  $parameters
    ->setMinDepth($level);

  // We only want the current level.
  $parameters
    ->setMaxDepth($level);

  // We only want enabled links.
  $parameters
    ->onlyEnabledLinks();
  $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);
  $config = $this
    ->getConfiguration();
  $language = \Drupal::languageManager()
    ->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)
    ->getId();
  foreach ($tree as $element) {

    // Skip inaccessible links.
    if ($element->link instanceof InaccessibleMenuLink) {
      continue;
    }
    $url = $element->link
      ->getUrlObject();

    // Only try to get route parameters from routed links.
    if ($url
      ->isRouted()) {
      $routeParams = $url
        ->getRouteParameters();
      reset($routeParams);
      $entity_type = key($routeParams);
      if (in_array($entity_type, $this
        ->getEntityTypes())) {

        // The link is an entity link.
        $entity = $this->entityTypeManager
          ->getStorage($entity_type)
          ->load($routeParams[$entity_type]);
        if ($this
          ->getConfigurationValue($config, 'only_current_language') == 1) {
          $languages = $entity
            ->getTranslationLanguages();
          if (!array_key_exists($language, $languages)) {

            // Skip this entity as content is not translated in current language.
            continue;
          }
        }

        // Get render array and continue to next menu item.
        $build['#menu_items'][] = $this->entityTypeManager
          ->getViewBuilder($entity_type)
          ->view($entity, $config['view_mode_' . $entity_type]);
        continue;
      }
    }

    // The link is a routed non-entity link or an external link.
    // If the configuration option is set, create a render array.
    if ($this
      ->getConfigurationValue($config, 'display_non_entities') == 1) {
      $build['#menu_items'][] = [
        '#theme' => 'entity_submenu_item',
        '#url' => $url,
        '#title' => $element->link
          ->getTitle(),
      ];
    }
  }
  return $build;
}