function theme_links

Returns HTML for a set of links.

Parameters

$variables: An associative array containing:

  • links: An associative array of links to be themed. The key for each link is used as its CSS class. Each link should be itself an array, with the following elements:

    • title: The link text.
    • href: The link URL. If omitted, the 'title' is shown as a plain text item in the links list.
    • html: (optional) Whether or not 'title' is HTML. If set, the title will not be passed through check_plain().
    • attributes: (optional) Attributes for the anchor, or for the <span> tag used in its place if no 'href' is supplied. If element 'class' is included, it must be an array of one or more class names.

    If the 'href' element is supplied, the entire link array is passed to l() as its $options parameter.

  • attributes: A keyed array of attributes for the UL containing the list of links.
  • heading: (optional) A heading to precede the links. May be an associative array or a string. If it's an array, it can have the following elements:

    • text: The heading text.
    • level: The heading level (e.g. 'h2', 'h3').
    • class: (optional) An array of the CSS classes for the heading.

    When using a string it will be used as the text of the heading and the level will default to 'h2'. Headings should be used on navigation menus and any list of links that consistently appears on multiple pages. To make the heading invisible use the 'element-invisible' CSS class. Do not use 'display:none', which removes it from screen-readers and assistive technology. Headings allow screen-reader and keyboard only users to navigate to or skip the links. See http://juicystudio.com/article/screen-readers-display-none.php and http://www.w3.org/TR/WCAG-TECHS/H42.html for more information.

Related topics

30 theme calls to theme_links()
ajax_test_dialog in drupal/core/modules/system/tests/modules/ajax_test/ajax_test.module
Menu callback: Renders a form elements and links with #ajax['dialog'].
book_node_view_link in drupal/core/modules/book/book.module
Adds relevant book links to the node's links.
CommentRenderController::buildContent in drupal/core/modules/comment/lib/Drupal/comment/CommentRenderController.php
Overrides Drupal\Core\Entity\EntityRenderController::buildContent().
CommentRenderController::buildContent in drupal/core/modules/comment/lib/Drupal/comment/CommentRenderController.php
Overrides Drupal\Core\Entity\EntityRenderController::buildContent().
comment_node_view in drupal/core/modules/comment/comment.module
Implements hook_node_view().

... See full list

File

drupal/core/includes/theme.inc, line 1686
The theme system, which controls the output of Drupal.

Code

function theme_links($variables) {
  $language_url = language(LANGUAGE_TYPE_URL);
  $links = $variables['links'];
  $attributes = $variables['attributes'];
  $heading = $variables['heading'];
  $output = '';
  if (!empty($links)) {

    // Prepend the heading to the list, if any.
    if (!empty($heading)) {

      // Convert a string heading into an array, using a H2 tag by default.
      if (is_string($heading)) {
        $heading = array(
          'text' => $heading,
        );
      }

      // Merge in default array properties into $heading.
      $heading += array(
        'level' => 'h2',
        'attributes' => array(),
      );

      // @todo Remove backwards compatibility for $heading['class'].
      if (isset($heading['class'])) {
        $heading['attributes']['class'] = $heading['class'];
      }
      $output .= '<' . $heading['level'] . new Attribute($heading['attributes']) . '>';
      $output .= check_plain($heading['text']);
      $output .= '</' . $heading['level'] . '>';
    }
    $output .= '<ul' . new Attribute($attributes) . '>';
    $num_links = count($links);
    $i = 0;
    foreach ($links as $key => $link) {
      $i++;
      $class = array();

      // Use the array key as class name.
      $class[] = drupal_html_class($key);

      // Add odd/even, first, and last classes.
      $class[] = $i % 2 ? 'odd' : 'even';
      if ($i == 1) {
        $class[] = 'first';
      }
      if ($i == $num_links) {
        $class[] = 'last';
      }

      // Handle links.
      if (isset($link['href'])) {
        $is_current_path = $link['href'] == current_path() || $link['href'] == '<front>' && drupal_is_front_page();
        $is_current_language = empty($link['language']) || $link['language']->langcode == $language_url->langcode;
        if ($is_current_path && $is_current_language) {
          $class[] = 'active';
        }

        // @todo Reconcile Views usage of 'ajax' as a boolean with the rest of
        //   core's usage of it as an array.
        if (isset($link['ajax']) && is_array($link['ajax'])) {

          // To attach Ajax behavior, render a link element, rather than just
          // call l().
          $link_element = array(
            '#type' => 'link',
            '#title' => $link['title'],
            '#href' => $link['href'],
            '#ajax' => $link['ajax'],
            '#options' => array_diff_key($link, drupal_map_assoc(array(
              'title',
              'href',
              'ajax',
            ))),
          );
          $item = drupal_render($link_element);
        }
        else {

          // Pass in $link as $options, they share the same keys.
          $item = l($link['title'], $link['href'], $link);
        }
      }
      else {

        // Merge in default array properties into $link.
        $link += array(
          'html' => FALSE,
          'attributes' => array(),
        );
        $item = '<span' . new Attribute($link['attributes']) . '>';
        $item .= $link['html'] ? $link['title'] : check_plain($link['title']);
        $item .= '</span>';
      }
      $output .= '<li' . new Attribute(array(
        'class' => $class,
      )) . '>';
      $output .= $item;
      $output .= '</li>';
    }
    $output .= '</ul>';
  }
  return $output;
}