theme_link()

(issue) The markup for generating a link is now handled by a theme function, theme_link(), instead of being hard-coded into the l() function. This allows you to implement a preprocess function or an override function in order to customize the markup of any link. Doing so may slow down Drupal pages that have many links, so it is recommended to evaluate the benefit vs. the performance trade-off, but the capability exists for sites and themes that need it. This theme function is for customizing the markup of links, generically. Other theme functions exist for customizing links based on context. For example, to customize menu links, override theme_menu_link() instead.

Example of hook_preprocess_link():

<?php
function mytheme_preprocess_link(&$variables) {
 
// In order to style links differently depending on what they are linking to,
  // add classes that contain information about the link path.
 
if (strpos($variables['path'], ':') !== FALSE) {
   
// For external links, add a class indicating an external link and a class
    // indicating the scheme (e.g., for 'mailto:...' links, add a 'link-mailto'
    // class).
   
$variables['options']['attributes']['class'][] = 'link-external';
   
$variables['options']['attributes']['class'][] = 'link-' . parse_url($variables['path'], PHP_URL_SCHEME);
  }
  else {
   
// For internal paths, add a class indicating an internal link.
   
$variables['options']['attributes']['class'][] = 'link-internal';
    if (empty(
$variables['path']) || $variables['path'] == '<front>') {
     
// Add a class indicating a link to the front page.
     
$variables['options']['attributes']['class'][] = 'link-front';
    }
    else {
     
// For internal links not to the front page, add a class for each part
      // of the path. For example, for a link to 'admin/structure/block', add
      // the classes 'link-path-admin', 'link-path-admin-structure', and
      // 'link-path-admin-structure-block'.
     
$class = 'link-path';
      foreach (
explode('/', $variables['path']) as $path_part) {
       
$class .= '-' . $path_part;
       
$variables['options']['attributes']['class'][] = $class;
      }
    }
  }
}
?>

Example of overriding theme_link():

<?php
function mytheme_link($variables) {
 
// Place a span within and outside the anchor tag in order to implement some
  // special styling.
 
return '<span class="link-wrapper"><a href="' . check_plain(url($path, $options)) . '"' . drupal_attributes($options['attributes']) . '><span class="link-content-wrapper">' . ($options['html'] ? $text : check_plain($text)) . '</span></a></span>';
}
?>

Syndicate content