You are here

function _toolbar_do_get_rendered_subtrees in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/toolbar/toolbar.module \_toolbar_do_get_rendered_subtrees()

#pre_render callback for toolbar_get_rendered_subtrees().

1 string reference to '_toolbar_do_get_rendered_subtrees'
toolbar_get_rendered_subtrees in core/modules/toolbar/toolbar.module
Returns the rendered subtree of each top-level toolbar link.

File

core/modules/toolbar/toolbar.module, line 304
Administration toolbar for quick access to top level administration items.

Code

function _toolbar_do_get_rendered_subtrees(array $data) {
  $menu_tree = \Drupal::service('toolbar.menu_tree');

  // Load the administration menu. The first level is the "Administration" link.
  // In order to load the children of that link and the subsequent two levels,
  // start at the second level and end at the fourth.
  $parameters = new MenuTreeParameters();
  $parameters
    ->setMinDepth(2)
    ->setMaxDepth(4)
    ->onlyEnabledLinks();

  // @todo Make the menu configurable in https://www.drupal.org/node/1869638.
  $tree = $menu_tree
    ->load('admin', $parameters);
  $manipulators = array(
    array(
      'callable' => 'menu.default_tree_manipulators:checkAccess',
    ),
    array(
      'callable' => 'menu.default_tree_manipulators:generateIndexAndSort',
    ),
    array(
      'callable' => 'toolbar_menu_navigation_links',
    ),
  );
  $tree = $menu_tree
    ->transform($tree, $manipulators);
  $subtrees = array();

  // Calculated the combined cacheability of all subtrees.
  $cacheability = new CacheableMetadata();
  foreach ($tree as $element) {

    /** @var \Drupal\Core\Menu\MenuLinkInterface $link */
    $link = $element->link;
    if ($element->subtree) {
      $subtree = $menu_tree
        ->build($element->subtree);
      $output = \Drupal::service('renderer')
        ->renderPlain($subtree);
      $cacheability = $cacheability
        ->merge(CacheableMetadata::createFromRenderArray($subtree));
    }
    else {
      $output = '';
    }

    // Many routes have dots as route name, while some special ones like <front>
    // have <> characters in them.
    $url = $link
      ->getUrlObject();
    $id = str_replace(array(
      '.',
      '<',
      '>',
    ), array(
      '-',
      '',
      '',
    ), $url
      ->isRouted() ? $url
      ->getRouteName() : $url
      ->getUri());
    $subtrees[$id] = $output;
  }

  // Store the subtrees, along with the cacheability metadata.
  $cacheability
    ->applyTo($data);
  $data['#subtrees'] = $subtrees;
  return $data;
}