protected function MenuParentFormSelector::parentSelectOptionsTreeWalk

protected MenuParentFormSelector::parentSelectOptionsTreeWalk(array $tree, $menu_name, $indent, array &$options, $exclude, $depth_limit, CacheableMetadata &$cacheability = NULL)

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);
      }
    }
  }
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Menu!MenuParentFormSelector.php/function/MenuParentFormSelector::parentSelectOptionsTreeWalk/8.1.x