1 common.inc l($text, $path, array $options = array())

Formats an internal or external URL link as an HTML anchor tag.

This function correctly handles aliased paths and adds an 'active' class attribute to links that point to the current page (for theme development), so all internal links output by modules should be generated by this function if possible.

However, for links enclosed in translatable text you should use t() and embed the HTML anchor tag directly in the translated string. For example:

t('Visit the <a href="@url">settings</a> page', array('@url' => url('admin')));

This keeps the context of the link title ('settings' in the example) for translators.

Parameters

string $text: The translated link text for the anchor tag.

string $path: The internal path or external URL being linked to, such as "node/34" or "http://example.com/foo". After the url() function is called to construct the URL from $path and $options, the resulting URL is passed through check_plain() before it is inserted into the HTML anchor tag, to ensure well-formed HTML. See url() for more information and notes.

array $options: An associative array of additional options. Defaults to an empty array. It may contain the following elements.

  • 'attributes': An associative array of HTML attributes to apply to the anchor tag. If element 'class' is included, it must be an array; 'title' must be a string; other elements are more flexible, as they just need to work in a call to backdrop_attributes($options['attributes']).
  • 'html' (default FALSE): Whether $text is HTML or just plain-text. For example, to make an image tag into a link, this must be set to TRUE, or you will see the escaped HTML image tag. $text is not sanitized if 'html' is TRUE. The calling function must ensure that $text is already safe.
  • 'language': An optional language object. If the path being linked to is internal to the site, $options['language'] is used to determine whether the link is "active", or pointing to the current page (the language as well as the path must match). This element is also used by url().
  • Additional $options elements used by the url() function.

Return value

string: An HTML string containing a link to the given path.

See also

url()

theme_link()

File

core/includes/common.inc, line 2889
Common functions that many Backdrop modules will need to reference.

Code

function l($text, $path, array $options = array()) {
  global $language_url;

  // Merge in default options.
  $options += array(
    'attributes' => array(),
    'query' => array(),
    'html' => FALSE,
    'language' => NULL,
  );

  // Append active class.
  if (($path == $_GET['q'] || ($path == '<front>' && backdrop_is_front_page())) && 
    (empty($options['language']) || $options['language']->langcode == $language_url->langcode)) {
    $options['attributes']['class'][] = 'active';
    $options['attributes']['aria-current'] = 'page';
  }

  // Remove all HTML and PHP tags from a tooltip, calling expensive strip_tags()
  // only when a quick strpos() gives suspicion tags are present.
  if (isset($options['attributes']['title']) && strpos($options['attributes']['title'], '<') !== FALSE) {
    $options['attributes']['title'] = strip_tags($options['attributes']['title']);
  }

  // Move attributes out of options. url() doesn't need them.
  $attributes = backdrop_attributes($options['attributes']);
  unset($options['attributes']);

  $url = url($path, $options);
  $skip_js_paths = array(
    'javascript:void()',
    'javascript:void();',
    'javascript:void(0)',
    'javascript:void(0);',
  );
  if (!(is_null($path) || in_array(strtolower($path), $skip_js_paths))) {
    $url = check_url($url);
  }

  // Sanitize the link text if necessary.
  if (!$options['html']) {
    $text = check_plain($text);
  }

  return '<a href="' . $url . '"' . $attributes . '>' . $text . '</a>';
}