Theming the $links variable in Drupal nodes

<?php
function mymodule_link_alter(&$node, &$links) {
  foreach (
$links as $module => $link) {   // iterate over the $links array
    //drupal_set_message(print_r($links)); // uncomment to display your $links array

    // check if this element is the forward module's link
   
if ($module == 'forward_links') {
     
$title = t('Email this page to a friend');    // change the title to suit
     
$path = path_to_theme() . '/images/email.png'// make an image path

      // now update the links array
      // set the title to some html of the image and choice of link text
     
$links[$module]['title'] = theme('image', $path, $title, $title) . ' Email';

     
// let's set some attributes on the link
     
$links[$module]['attributes'] = array(
       
'title' => $title,
       
'class' => 'forward-page',
       
'rel' => 'nofollow',
      );

     
// this must be set, so that l() interprets the image tag correctly
     
$links[$module]['html'] = TRUE;
    }
  }
}
?>

<?php
function _phptemplate_variables($hook, $vars = array()) {
  switch (
$hook) {
    case
'node':
      foreach (
$vars['node']->links as $module => $link) {
        if (
$module == 'forward_links') {
         
$title = t('Email this page to a friend');
         
$path = path_to_theme() . '/images/email.png';
         
$vars['node']->links[$module]['title'] =
             
theme('image', $path, $title, $title) . ' Email';
         
$vars['node']->links[$module]['attributes'] =
              array(
'title' => $title, 'class' => 'forward-page', 'rel' => 'nofollow');
         
$vars['node']->links[$module]['html'] = TRUE;
        }
      }
     
$vars['links'] = theme('links', $vars['node']->links,
                                           array(
'class' => 'links inline'));
      break;
  }
}
?>

Blog Tags: DrupalThemesModules

Comments

<?php
$links
= str_replace('>Printer-friendly version<', ' title="Click to view a printer-friendly version of this document."><img src="images/printer.png"><',$links);
?>

As the print module maintainer, I would like to point out that there's better solutions in the case of the print module than either of the above. You can theme the $links link of the print module to your heart's content using the theme_print_format_link() function.

See more about that in #4 of http://drupal.org/node/190173.

hi,

is it possible to do this without making a module? somewhere in phptemplate for example.

<?php
 
function _phptemplate_variables($hook, $vars = array()) {
  switch (
$hook) {
    case
'node':
    foreach (
$vars['node']->links as $module => $link) {
      if (
$module == 'forward_links') {
       
// alter stuff here
     
}
    }
  }
}
?>

hi, I want to theme $links in comments

in _phptemplate_variables function, I add following code

case 'comment':
print_r($vars['comment']->links);
break;
nothing shows up, probably theres no such variable called $vars['comment']->links.

can you please show me how theme it in comment. I got stuck here for 2 days.

thank you so much for ur help.

does this work in drupal 6?

For D6 you should probably use YOURTHEME_preprocess_node instead of _phptemplate_variables though.

Thanks for this article, it worked for me

It is easy to do it for my website, but I found that if I use _preprocess_node(&$vars, $hook) then I can not get the node ID in the function.

Add new comment