| 7 theme.inc | theme_link($variables) |
| 8 theme.inc | theme_link($variables) |
Returns HTML for a link.
All Drupal code that outputs a link should call the l() function. That function performs some initial preprocessing, and then, if necessary, calls theme('link') for rendering the anchor tag.
To optimize performance for sites that don't need custom theming of links, the l() function includes an inline copy of this function, and uses that copy if none of the enabled modules or the active theme implement any preprocess or process functions or override this theme implementation.
Parameters
$variables: An associative array containing the keys 'text', 'path', and 'options'. See the l() function for information about these variables.
See also
l()
Related topics
- l in includes/
common.inc - Formats an internal or external URL link as an HTML anchor tag.
- book_block_view in modules/
book/ book.module - Implements hook_block_view().
- dblog_overview in modules/
dblog/ dblog.admin.inc - Page callback: Displays a listing of database log messages.
- l in includes/
common.inc - Formats an internal or external URL link as an HTML anchor tag.
File
- includes/
theme.inc, line 1633 - The theme system, which controls the output of Drupal.
Code
function theme_link($variables) {
return '<a href="' . check_plain(url($variables['path'], $variables['options'])) . '"' . drupal_attributes($variables['options']['attributes']) . '>' . ($variables['options']['html'] ? $variables['text'] : check_plain($variables['text'])) . '</a>';
}
Comments
Note: A link render array
PermalinkNote: A link render array requires the HTML option, even though it is optional in the l() function
$link = array('#theme' => 'link',
'#text' => 'Click Here',
'#path' => $path,
'#options' => array(
'attributes' => array('class' => array('cool-class'), 'id' => 'cool-id'),
//REQUIRED:
'html' => FALSE,
),
);
A bug?
PermalinkThis smells buggy to me. If the 'attributes' array has elements in it then 'html' doesn't have to exist. But if 'attributes' is an empty array then Drupal will complain when there is not 'html' element set. Seems odd that '#options' aren't optional.
When I use pound signs in the
PermalinkWhen I use pound signs in the array keys I get fatal errors. It works without the pound signs though...
Perhaps the other comments
PermalinkPerhaps the other comments here relate to an older version of the function, but the current way to use this in a render array is something like:
<?php$img = array(
'#theme' => 'image_style',
'#style_name' => 'avatar_80x80',
'#path' => $uri,
);
$render = array(
'#theme' => 'link',
'#text' => drupal_render($img),
'#path' => "node/" . $node->nid,
'#options' => array(
'attributes' => array(),
'html' => TRUE,
),
);
?>
Remember to set html to FALSE if #text is not already sanitized.
Link to issue working to address this
Permalinkhttp://drupal.org/node/1187032
Main menu (or any other) with visible description
PermalinkHow to display description on menus in Drupal 7:
Just copy paste this code to template.php in your theme dir and rename MYTHEME_links. Remember to remove ending '?>'
<?php
function MYTHEME_links($variables) {
$links = $variables['links'];
$attributes = $variables['attributes'];
$heading = $variables['heading'];
global $language_url;
$output = '';
if (
count($links) > 0) {$output = '';
// Treat the heading first if it is present to prepend it to the// list of links.
if (!empty($heading)) {
if (is_string($heading)) {
// Prepare the array that will be used when the passed heading
// is a string.
$heading = array(
'text' => $heading,
// Set the default level of the heading.
'level' => 'h2',
);
}
$output .= '<' . $heading['level'];
if (!empty($heading['class'])) {
$output .= drupal_attributes(array('class' => $heading['class']));
}
$output .= '>' . check_plain($heading['text']) . '</' . $heading['level'] . '>';
}
$output .= '<ul' . drupal_attributes($attributes) . '>';
$num_links = count($links);$i = 1;
foreach (
$links as $key => $link) {$class = array($key);
// Add first, last and active classes to the list of links to help out themers.if ($i == 1) {
$class[] = 'first';
}
if ($i == $num_links) {
$class[] = 'last';
}
if (isset($link['href']) && ($link['href'] == $_GET['q'] || ($link['href'] == '<front>' && drupal_is_front_page()))
&& (empty($link['language']) || $link['language']->language == $language_url->language)) {
$class[] = 'active';
}
$output .= '<li' . drupal_attributes(array('class' => $class)) . '>';
if (isset(
$link['href'])) {// Pass in $link as $options, they share the same keys.
// $attributes['id'] - menu name change it from 'main-menu' to your menu name if you like to display description in some other menu
if(isset($attributes['id'])&& $attributes['id']=='main-menu') {
$link['html'] = true;
$output .= l($link['title']. '<span>' . $link['attributes']['title'] . '</span>',$link['href'], $link);}
else {
$output .= l($link['title'], $link['href'], $link);
}
}
elseif (!empty($link['title'])) {
// Some links are actually not links, but we wrap these in <span> for adding title and class attributes.
if (empty($link['html'])) {
$link['title'] = check_plain($link['title']);
}
$span_attributes = '';
if (isset($link['attributes'])) {
$span_attributes = drupal_attributes($link['attributes']);
}
$output .= '<span' . $span_attributes . '>' . $link['title'] . '</span>';
}
$i++;$output .= "</li>\n";
}
$output .= '</ul>';}
return $output;
}
?>
This solution is based on Drupal 6 version from http://ivansotof.com/2009/07/visible-description-on-drupal-primary-links
Didn't work for Sub theme!
PermalinkI used the code by rolkos
The YOURTHEMENAME_link function worked fine in Zen theme but didn't work at subtheme.
I tried the following:
<?phpfunction YOURTHEMENAME_link($variables) {
print_r($variables); die;
}
?>
Did work on Zen theme but didn't work on sub themes based on Zen themes. Any thing I missed?
Theme Wrapper
PermalinkIs there a link theme wrapper so I can wrap an image in link?
You're looking at it.
PermalinkSee http://api.drupal.org/api/drupal/includes%21theme.inc/function/theme_lin...
HTML in menu links
PermalinkTo add HTML tags in menu title add this to template.php
<?phpfunction YOURTHEMENAME_link($variables) {
$variables['options']['html'] = TRUE;
return '<a href="' . check_plain(url($variables['path'], $variables['options'])) . '"' . drupal_attributes($variables['options']['attributes']) . '>' . ($variables['options']['html'] ? $variables['text'] : check_plain($variables['text'])) . '</a>';
}
?>
Of course replace YOURTHEMENAME with your theme name.
Consider also the link render array type
PermalinkConfusingly, Drupal provides a special type of render arrays for links-- instead of '#theme' => 'link' you can use '#type' => 'link' -- and more confusingly, it uses different parameters to do basically the same thing.
<?php$build['outlink'] = array(
'#type' => 'link',
'#title' => 'Central help and support site',
'#href' => 'http://help.example.com',
);
?>
See http://api.drupal.org/api/drupal/includes%21common.inc/function/drupal_p...
Query is escaped
PermalinkCaution: when adding 'query' to 'options' array, the
&is changed to&due tocheck_plain().