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 theming), 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.
string|array $text: The link text for the anchor tag as a translated string or render array.
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.
string An HTML string containing a link to the given path.
url()
function l($text, $path, array $options = array()) {
// Start building a structured representation of our link to be altered later.
$variables = array(
'text' => is_array($text) ? drupal_render($text) : $text,
'path' => $path,
'options' => $options,
);
// Merge in default options.
$variables['options'] += array(
'attributes' => array(),
'query' => array(),
'html' => FALSE,
'language' => NULL,
);
// Because l() is called very often we statically cache values that require an
// extra function call.
static $drupal_static_fast;
if (!isset($drupal_static_fast['active'])) {
$drupal_static_fast['active'] =& drupal_static(__FUNCTION__);
}
$active =& $drupal_static_fast['active'];
if (!isset($active)) {
$active = array(
'path' => current_path(),
'front_page' => drupal_is_front_page(),
'language' => language(Language::TYPE_URL)->langcode,
'query' => Drupal::service('request')->query
->all(),
);
}
// Determine whether this link is "active', meaning that it links to the
// current page. It is important that we stop checking "active" conditions if
// we know the link is not active. This helps ensure that l() remains fast.
// An active link's path is equal to the current path.
$variables['url_is_active'] = ($path == $active['path'] || $path == '<front>' && $active['front_page']) && (empty($variables['options']['language']) || $variables['options']['language']->langcode == $active['language']) && $variables['options']['query'] == $active['query'];
// Add the "active" class if appropriate.
if ($variables['url_is_active']) {
$variables['options']['attributes']['class'][] = 'active';
}
// 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($variables['options']['attributes']['title']) && strpos($variables['options']['attributes']['title'], '<') !== FALSE) {
$variables['options']['attributes']['title'] = strip_tags($variables['options']['attributes']['title']);
}
// Allow other modules to modify the structure of the link.
Drupal::moduleHandler()
->alter('link', $variables);
// Move attributes out of options. url() doesn't need them.
$attributes = new Attribute($variables['options']['attributes']);
unset($variables['options']['attributes']);
// The result of url() is a plain-text URL. Because we are using it here
// in an HTML argument context, we need to encode it properly.
$url = check_plain(url($variables['path'], $variables['options']));
// Sanitize the link text if necessary.
$text = $variables['options']['html'] ? $variables['text'] : check_plain($variables['text']);
return '<a href="' . $url . '"' . $attributes . '>' . $text . '</a>';
}