You are here

protected function MenuParentFormSelector::parentSelectOptionsTreeWalk in Drupal 10

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Menu/MenuParentFormSelector.php \Drupal\Core\Menu\MenuParentFormSelector::parentSelectOptionsTreeWalk()
  2. 9 core/lib/Drupal/Core/Menu/MenuParentFormSelector.php \Drupal\Core\Menu\MenuParentFormSelector::parentSelectOptionsTreeWalk()

Iterates over all items in the tree to prepare the parents select options.

Parameters

\Drupal\Core\Menu\MenuLinkTreeElement[] $tree: The menu tree.

string $menu_name: The menu name.

string $indent: The indentation string used for the label.

array $options: The select options.

string $exclude: An excluded menu link.

int $depth_limit: The maximum depth of menu links considered for the select options.

\Drupal\Core\Cache\CacheableMetadata|null &$cacheability: The object to add cacheability metadata to, if not NULL.

File

core/lib/Drupal/Core/Menu/MenuParentFormSelector.php, line 141

Class

MenuParentFormSelector
Default implementation of the menu parent form selector service.

Namespace

Drupal\Core\Menu

Code

protected function parentSelectOptionsTreeWalk(array $tree, $menu_name, $indent, array &$options, $exclude, $depth_limit, CacheableMetadata &$cacheability = NULL) {
  foreach ($tree as $element) {
    if ($element->depth > $depth_limit) {

      // Don't iterate through any links on this level.
      break;
    }

    // Collect the cacheability metadata of the access result, as well as the
    // link.
    if ($cacheability) {
      $cacheability = $cacheability
        ->merge(CacheableMetadata::createFromObject($element->access))
        ->merge(CacheableMetadata::createFromObject($element->link));
    }

    // Only show accessible links.
    if (!$element->access
      ->isAllowed()) {
      continue;
    }
    $link = $element->link;
    if ($link
      ->getPluginId() != $exclude) {
      $title = $indent . ' ' . Unicode::truncate($link
        ->getTitle(), 30, TRUE, FALSE);
      if (!$link
        ->isEnabled()) {
        $title .= ' (' . $this
          ->t('disabled') . ')';
      }
      $options[$menu_name . ':' . $link
        ->getPluginId()] = $title;
      if (!empty($element->subtree)) {
        $this
          ->parentSelectOptionsTreeWalk($element->subtree, $menu_name, $indent . '--', $options, $exclude, $depth_limit, $cacheability);
      }
    }
  }
}