function NavigationMenuLinkTreeManipulators::shouldAddOverviewLink

Whether a menu tree element should have an overview link added to it.

Parameters

\Drupal\Core\Menu\MenuLinkTreeElement $element: The menu link tree element to check.

Return value

bool TRUE if menu tree element should have a child overview link added.

1 call to NavigationMenuLinkTreeManipulators::shouldAddOverviewLink()
NavigationMenuLinkTreeManipulators::addSecondLevelOverviewLinks in core/modules/navigation/src/Menu/NavigationMenuLinkTreeManipulators.php
Adds an "overview" child link to second level menu links with children.

File

core/modules/navigation/src/Menu/NavigationMenuLinkTreeManipulators.php, line 74

Class

NavigationMenuLinkTreeManipulators
Provides a menu link tree manipulator for the navigation menu block.

Namespace

Drupal\navigation\Menu

Code

protected function shouldAddOverviewLink(MenuLinkTreeElement $element) : bool {
  if (empty($element->subtree) || !$this->isEnabledAndAccessible($element)) {
    return FALSE;
  }
  $route_name = $element->link
    ->getRouteName();
  if (in_array($route_name, [
    '<nolink>',
    '<button>',
  ])) {
    return FALSE;
  }
  $has_visible_children = FALSE;
  foreach ($element->subtree as $sub_element) {
    // Do not add overview link if there are no accessible or enabled
    // children.
    if ($this->isEnabledAndAccessible($sub_element)) {
      $has_visible_children = TRUE;
    }
    // Do not add overview link if there is already a child linking to the
    // same URL.
    if ($sub_element->link
      ->getRouteName() === $route_name) {
      return FALSE;
    }
  }
  if (!$has_visible_children) {
    return FALSE;
  }
  // The systemAdminMenuBlockPage() method in SystemController returns a list
  // of child menu links for the page. If the second-level menu item link's
  // route uses that controller, do not add the overview link, because that
  // duplicates what is already in the navigation menu.
  try {
    $controller = ltrim($this->routeProvider
      ->getRouteByName($route_name)
      ->getDefault('_controller') ?? '', "\\");
    return $controller !== SystemController::class . '::systemAdminMenuBlockPage';
  } catch (RouteNotFoundException) {
    return TRUE;
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.