function hook_link_alter

You are here

5 core.php hook_link_alter(&$node, &$links)
6 core.php hook_link_alter(&$links, $node, $comment = NULL)
8 system.api.php hook_link_alter(&$variables)

Perform alterations before links on a node or comment are rendered.

One popular use of this hook is to modify/remove links from other modules. If you want to add a link to the links section of a node or comment, use hook_link() instead.

Parameters

$links: Nested array of links for the node or comment keyed by providing module.

$node: A node object.

$comment: An optional comment object if the links are comment links. If not provided, the links are node links.

See also

hook_link()

Related topics

4 functions implement hook_link_alter()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

hook_menu_link_alter in developer/hooks/core.php
Alter the data being saved to the {menu_links} table by menu_link_save().
hook_translated_menu_link_alter in developer/hooks/core.php
Alter a menu link after it's translated, but before it's rendered.
hook_translation_link_alter in developer/hooks/core.php
Perform alterations on translation links.
translation_translation_link_alter in modules/translation/translation.module
Implementation of hook_translation_link_alter().
5 invocations of hook_link_alter()
comment_render in modules/comment/comment.module
Renders comment(s).
node_view in modules/node/node.module
Generate a display of the given node.
taxonomy_link in modules/taxonomy/taxonomy.module
Implementation of hook_link().
theme_comment_flat_expanded in modules/comment/comment.module
Theme comment flat expanded view.
theme_comment_thread_expanded in modules/comment/comment.module
Theme comment thread expanded view.

File

developer/hooks/core.php, line 1048
These are the hooks that are invoked by the Drupal core.

Code

function hook_link_alter(&$links, $node, $comment = NULL) {
  foreach ($links as $module => $link) {
    if (strstr($module, 'taxonomy_term')) {
      // Link back to the forum and not the taxonomy term page
      $links[$module]['href'] = str_replace('taxonomy/term', 'forum', $link['href']);
    }
  }
}

Comments

In Drupal 7, links can be modified in hook_node_view_alter().

i don't agree that hook_node_view_alter() is a valid replacement for this in D7 as it only runs when displaying a node; which doesn't include many pages. So far i haven't found a decent hook to do this in D7.

hook_entity_view_alter()

This can be done at the theme layer too. Use THEMENAME_links() for all links, or the theme suggestions to target specific links.

<?php
/**
* Intercepts the node links to alter the user blog links.
* @param unknown $variables
* @return string
*/
function THEMENAME_links__node($variables) {
 
$links = &$variables['links'];
  if (isset(
$links['blog_usernames_blog'])) {
   
$links['blog_usernames_blog'] = array(
     
'title' => t('Blogs'),
     
'href' => 'blog',
     
'attributes' => array('Read the latest blog entries.'),
    );
  }
  return
theme_links($variables);
}
?>

function mymodule_link_alter(&$links, $node, $comment = NULL) {
// move stats to the end
$move=$links['statistics_counter'];
unset($links['statistics_counter']);
$links['statistics_counter']=$move;
//dsm($links);
}

Prior to Drupal 6.16, hook_link_alter was never used for comments except with single view (such as when replying to a specific comment). The optional $comment variable was added in Drupal 6.17. These changes were made based on #374463: Alter comment links.