8

I am using Drupal 6 and would like to modify the $links variable to include other links programmatically. I have searched high and low on Google on how to do this to no avail. Does someone know how to do this?

22

Drupal 6

In Drupal 6, you can't use hooks or alter hooks inside themes, so you'll need to create a custom module to modify the links. Once done, you'll want to implement hook_link() (if you're creating a new link) or hook_link_alter() (if you want to modify an existing link).

Adding a new link:

example_link($type, $object, $teaser = FALSE) {
  $links = array();
  // Add a custom link to nodes
  if ($type == 'node') {
    $links['example_mylink'] = array(
      'title' => t('Test link'),
      'href' => 'foo',
      'attributes' => array(
        'title' => 'Test link',
      ),
    );
  }
  return $links;
}

Modifying existing links:

example_link_alter(&$links, $node, $comment = NULL) {
  // Remove the read more link
  unset($links['node']['node_read_more']);

  // Change the title of the read more link
  $links['node']['node_read_more']['title'] = t('More information');

  // Move read more link to first slot
  $link_read_more = $links['node']['node_read_more'];
  unset($links['node']['node_read_more']);
  $links = $links['node'];
  $links['node'] = array(
    'node_read_more' => $link_read_more,
  ) + $links;

  // Move link to the last slot
  $link_read_more = $links['node']['node_read_more'];
  unset($links['node']['node_read_more']);
  $links['node']['node_read_more'] = $link_read_more;
}

Drupal 7

In Drupal 7, this is a little more straightforward, as themes can implement alter hooks. The alter hook you're looking for is hook_node_view_alter():

function example_node_view_alter(&$build) {
  // Remove the read more link
  unset($build['links']['node']['#links']['node-readmore']);

  // Add your own custom link
  $build['links']['node']['#links']['example-mylink'] = array(
    'title' => t('Test link'), 
    'href' => 'foo', 
    'html' => TRUE, 
    'attributes' => array(
      'title' => 'Test link',
    ),
  );

  // Move read more link to first slot
  $link_read_more = $build['links']['node']['#links']['node_read_more'];
  unset($build['links']['node']['#links']['node_read_more']);
  $links = $build['links']['node']['#links'];
  $build['links']['node']['#links'] = array(
    'node_read_more' => $link_read_more,
  ) + $links;

  // Move link to the last slot
  $link_read_more = $build['links']['node']['#links']['node_read_more'];
  unset($build['links']['node']['#links']['node_read_more']);
  $build['links']['node']['#links']['node_read_more'] = $link_read_more;
}

You can place this directly in your template.php file.

| improve this answer | |
  • Thanks, this is exactly what I was looking for! You saved me from implementing an interesting working around using the template.php file for Drupal 6. Would this solution work with content generated by views? – user5013 Jan 16 '12 at 21:34
  • @user5013 If you're thinking about the full node or teaser display in a view, it should work out of the box. If you wanted to add a link to a view, you can do that too: just change the value of 'href' to whatever you'd like. – user7 Jan 16 '12 at 21:36
  • No I was thinking about if this solution would work with the fields display in a view. – user5013 Jan 16 '12 at 22:09
  • @user5013 Ah, then in that case, no: the links are only added when the node is fully built. Many of the links that usually show up in the links bar are available as separate fields in Views, though. – user7 Jan 16 '12 at 22:16
  • 1
    @MotoTribe links are rendered in the same order they appear in the array, so it's just straight array manipulation to change the order. Added examples. – user7 Jan 22 '12 at 22:23
1

Assuming you are talking about links that get rendered on node.tpl.php, you will need to implement hook_link for example look at node_link. And if you are new to drupal hooks then this need to happen in a custom module, so lets say your custom module name is "foo" you will need to write a function called foo_link which should have same arguments as hook_link.

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.