You are here

function theme_admin_menu_links in Administration menu 8.3

Same name and namespace in other branches
  1. 6.3 admin_menu.module \theme_admin_menu_links()
  2. 7.3 admin_menu.module \theme_admin_menu_links()

Render a themed list of links.

Parameters

$variables:

  • elements: A renderable array of links using the following keys:

    • #attributes: Optional array of attributes for the list item, processed via Drupal\Core\Template\Attribute.
    • #title: Title of the link, passed to l().
    • #href: Optional path of the link, passed to l(). When omitted, the element's '#title' is rendered without link.
    • #description: Optional alternative text for the link, passed to l().
    • #options: Optional alternative text for the link, passed to l().

    The array key of each child element itself is passed as path for l().

4 theme calls to theme_admin_menu_links()
admin_menu_links_account in ./admin_menu.inc
Builds the account links.
admin_menu_links_icon in ./admin_menu.inc
Build icon menu links; mostly containing maintenance helpers.
admin_menu_links_search in ./admin_menu.inc
Build search widget.
admin_menu_links_users in ./admin_menu.inc
Builds user counter.

File

./admin_menu.module, line 630
Render an administrative menu as a dropdown menu at the top of the window.

Code

function theme_admin_menu_links($variables) {
  $destination =& drupal_static('admin_menu_destination');
  $elements = $variables['elements'];
  if (!isset($destination)) {
    $destination = drupal_get_destination();
    $destination = $destination['destination'];
  }

  // The majority of items in the menu are sorted already, but since modules
  // may add or change arbitrary items anywhere, there is no way around sorting
  // everything again. element_sort() is not sufficient here, as it
  // intentionally retains the order of elements having the same #weight,
  // whereas menu links are supposed to be ordered by #weight and #title.
  uasort($elements, 'admin_menu_element_sort');
  $elements['#sorted'] = TRUE;
  $output = '';
  foreach (element_children($elements) as $path) {

    // Early-return nothing if user does not have access.
    if (isset($elements[$path]['#access']) && !$elements[$path]['#access']) {
      continue;
    }
    $elements[$path] += [
      '#attributes' => [],
      '#options' => [],
    ];

    // Render children to determine whether this link is expandable.
    if (isset($elements[$path]['#type']) || isset($elements[$path]['#theme']) || isset($elements[$path]['#pre_render'])) {
      $elements[$path]['#children'] = \Drupal::service('renderer')
        ->render($elements[$path]);
    }
    else {
      $elements[$path]['#children'] = theme('admin_menu_links', [
        'elements' => $elements[$path],
      ]);
      if (!empty($elements[$path]['#children'])) {
        $elements[$path]['#attributes']['class'][] = 'expandable';
      }
      if (isset($elements[$path]['#attributes']['class'])) {
        $elements[$path]['#attributes']['class'] = $elements[$path]['#attributes']['class'];
      }
    }
    $link = '';

    // Handle menu links.
    if (isset($elements[$path]['#href'])) {

      // Strip destination query string from href attribute and apply a CSS class
      // for our JavaScript behavior instead.
      if (isset($elements[$path]['#options']['query']['destination']) && $elements[$path]['#options']['query']['destination'] == $destination) {
        unset($elements[$path]['#options']['query']['destination']);
        $elements[$path]['#options']['attributes']['class'][] = 'admin-menu-destination';
      }
      $link = l($elements[$path]['#title'], $elements[$path]['#href'], $elements[$path]['#options']);
    }
    elseif (!isset($elements[$path]['#type']) && isset($elements[$path]['#title'])) {
      if (!empty($elements[$path]['#options']['html'])) {
        $title = $elements[$path]['#title'];
      }
      else {
        $title = check_plain($elements[$path]['#title']);
      }
      $attributes = '';
      if (isset($elements[$path]['#options']['attributes'])) {
        $attributes = new Attribute($elements[$path]['#options']['attributes']);
      }
      $link = '<span' . $attributes . '>' . $title . '</span>';
    }
    $output .= '<li' . new Attribute($elements[$path]['#attributes']) . '>';
    $output .= $link . $elements[$path]['#children'];
    $output .= '</li>';
  }

  // @todo #attributes probably required for UL, but already used for LI.
  // @todo Use $element['#children'] here instead.
  if ($output) {
    $elements['#wrapper_attributes']['class'][] = 'dropdown';
    $attributes = new Attribute($elements['#wrapper_attributes']);
    $output = "\n" . '<ul' . $attributes . '>' . $output . '</ul>';
  }
  return $output;
}