Hi, thanks khalid for this module.

On my site's design I have a special block in wich I would like to print out the favorites widget. What changes would I need to make in order to make the favorites code avaiable in .tpl.php files, in a variable of its own... and to keep it from appearing in $links.

Regards,
JR

Comments

dotidentity’s picture

Anybody?

Laurentvw’s picture

I'm also looking for this. Thanks in advance for anyone who could help us.

jp.stacey’s picture

The $links variable is put together by PHPTemplate. It runs the following code to get it:

$node->links ? theme('links', $node->links, array('class' => 'links inline')) : ''

This states: if a node has links, then theme them in a 'links' context, with extra arguments in the array() at the end; if it doesn't have links, set $links to the empty string ''. The favorite_nodes module adds links to the node using the hook_links hook (in favorite_nodes.module).

If you want to render just the favorite_nodes links, try putting the following in node.tpl.php:

// Check function exists, so theme doesn't break if  favorite_nodes disabled
if ( function_exists('favorite_nodes_link') ) {
  // Same function PHPTemplate uses
  print theme(
    'links',
    // Call the hook_link from the favorite_nodes module directly
    favorite_nodes_link('node', $node), 
    array('class' => 'links inline favorite-links')
  );
}

Rendering everything except favorite links is a bit more tricky. Unfortunately, the $node->links array elements are just added to the end in order; there's no easy way of always spotting the favorite links except by checking each link's URL.

A hack to do this might go as follows (stick in node.tpl.php as above; transfer to a block only when you're sure it works!):

// Get links from the node
$node_links = $node->links;
// Loop over the existing node links
foreach ($node_links as $k => $v) {
  // If we can't find "favorite_nodes/" in the link, put this link onto the end of a new array
  // Note the triple === sign - this is because strpos() returns zero, and (0 == false) is a true comparison!
  if ( strpos($v['href'], 'favorite_nodes/') === false) {
    $node_links_new[] = $v;
  }
}

// Render the new link array as in the favorite_nodes code snippet above
if ( function_exists('favorite_nodes_link') ) {
  print theme(
    'links',
    $node_links_new, 
    array('class' => 'links inline favorite-links')
  );
}

All of this has the (minor) disadvantage that Drupal spends time creating $links for you, and then you create it a second time in two halves. There might be a way round that by using node_load() in a block, but it'd be messy.

Does this help at all?

madaxe’s picture

Although this is stated for D5 version, I used jp.stacey's second section of code in the node.tpl.php in drupal 6.

It still works.