function template_preprocess_links

Same name and namespace in other branches
  1. 9 core/includes/theme.inc \template_preprocess_links()
  2. 8.9.x core/includes/theme.inc \template_preprocess_links()
  3. 10 core/includes/theme.inc \template_preprocess_links()

Prepares variables for links templates.

Default template: links.html.twig.

Unfortunately links templates duplicate the "active" class handling of l() and LinkGenerator::generate() because it needs to be able to set the "active" class not on the links themselves (<a> tags), but on the list items (<li> tags) that contain the links. This is necessary for CSS to be able to style list items differently when the link is active, since CSS does not yet allow one to style list items only if it contains a certain element with a certain class. I.e. we cannot yet convert this jQuery selector to a CSS selector: jQuery('li:has("a.is-active")')

Parameters

array $variables: An associative array containing:

  • links: An array of links to be themed. Each link itself is an array, with the following elements:

    • title: The link text.
    • url: (optional) The \Drupal\Core\Url object to link to. If the 'url' element is supplied, the 'title' and 'url' are used to generate a link through \Drupal::linkGenerator()->generate(). All data from the link array other than 'title' and 'url' are added as #options on the URL object. See \Drupal\Core\Url::fromUri() for details on the options. If no 'url' is supplied, the 'title' is printed as plain text.
    • attributes: (optional) Attributes for the anchor, or for the <span> tag used in its place if no 'url' is supplied. If element 'class' is included, it must be an array of one or more class names.
  • attributes: A keyed array of attributes for the <ul> containing the list of links.
  • set_active_class: (optional) Whether each link should compare the route_name + route_parameters or URL (path), language, and query options to the current URL, to determine whether the link is "active". If so, attributes will be added to the HTML elements for both the link and the list item that contains it, which will result in an "is-active" class being added to both. The class is added via JavaScript for authenticated users (in the active-link library), and via PHP for anonymous users (in the \Drupal\Core\EventSubscriber\ActiveLinkResponseFilter class).
  • 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').
    • attributes: (optional) An array of the CSS attributes 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 'visually-hidden' 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 https://www.w3.org/TR/WCAG-TECHS/H42.html for more information.

Deprecated

in drupal:11.2.0 and is removed from drupal:12.0.0. Initial template_preprocess functions are registered directly in hook_theme().

See also

https://www.drupal.org/node/3504125

\Drupal\Core\Utility\LinkGenerator

\Drupal\Core\Utility\LinkGenerator::generate()

system_page_attachments()

File

core/includes/theme.inc, line 452

Code

function template_preprocess_links(&$variables) : void {
  @trigger_error(__FUNCTION__ . '() is deprecated in drupal:11.2.0 and is removed from drupal:12.0.0. Initial template_preprocess functions are registered directly in hook_theme(). See https://www.drupal.org/node/3504125', E_USER_DEPRECATED);
  \Drupal::service(ThemePreprocess::class)->preprocessLinks($variables);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.