| 5 core.php | hook_link($type, |
| 6 core.php | hook_link($type, $object, $teaser = FALSE) |
Define internal Drupal links.
This hook enables modules to add links to many parts of Drupal. Links may be added in nodes or in the navigation block, for example.
The returned array should be a keyed array of link entries. Each link can be in one of two formats.
The first format will use the l() function to render the link:
- attributes: Optional. See l() for usage.
- fragment: Optional. See l() for usage.
- href: Required. The URL of the link.
- html: Optional. See l() for usage.
- query: Optional. See l() for usage.
- title: Required. The name of the link.
The second format can be used for non-links. Leaving out the href index will select this format:
- title: Required. The text or HTML code to display.
- attributes: Optional. An associative array of HTML attributes to apply to the span tag.
- html: Optional. If not set to true, check_plain() will be run on the title before it is displayed.
Parameters
$type: An identifier declaring what kind of link is being requested. Possible values:
- comment: Links to be placed below a comment being viewed.
- node: Links to be placed below a node being viewed.
$object: A node object or a comment object according to the $type.
$teaser: In case of node link: a 0/1 flag depending on whether the node is displayed with its teaser or its full form.
Return value
An array of the requested links.
Related topics
File
- developer/
hooks/ core.php, line 982 - These are the hooks that are invoked by the Drupal core.
Code
<?php
function hook_link($type, $object, $teaser = FALSE) {
$links = array();
if ($type == 'node' && isset($object->parent)) {
if (!$teaser) {
if (book_access('create', $object)) {
$links['book_add_child'] = array(
'title' => t('add child page'),
'href' => "node/add/book/parent/$object->nid",
);
}
if (user_access('see printer-friendly version')) {
$links['book_printer'] = array(
'title' => t('printer-friendly version'),
'href' => 'book/export/html/' . $object->nid,
'attributes' => array('title' => t('Show a printer-friendly version of this book page and its sub-pages.')),
);
}
}
}
$links['sample_link'] = array(
'title' => t('go somewhere'),
'href' => 'node/add',
'query' => 'foo=bar',
'fragment' => 'anchorname',
'attributes' => array('title' => t('go to another page')),
);
// Example of a link that's not an anchor
if ($type == 'video') {
if (variable_get('video_playcounter', 1) && user_access('view play counter')) {
$links['play_counter'] = array(
'title' => format_plural($object->play_counter, '1 play', '@count plays'),
);
}
}
return $links;
}
?> Login or register to post comments
Comments
Here is an example to link on
Here is an example to link on every node to the author profile (in my case are quicktabs on the profile).
<?php
function hook_link($type, $object, $teaser = FALSE) {
$links = array();
if ($object->type == 'video' && $object->name) {
$links['sample_link'] = array(
'title' => 'Videos by ' . $object->name,
'href' => 'user/' . $object->uid,
'query' => 'quicktabs_1=0',
'fragment' => 'quicktabs',
);
}
return $links;
}
?>
Weighting
As far as I've been able to work out, the $links array doesn't use a 'weight' array element on each item to allow custom ordering of the links.
My solution so far has been to manually reorder the array, with "lighter" items at the beginning, and "heavier" items at the end.
A little bit ick :(
How about hook_link_alter
Did you try hook_link_alter by any chance? It might do the trick, didn't test though so I can't guarantee. It would be a lot cleaner than messing with the template itself :)
Yes, i tried using the
Yes, i tried using the hook_link_alter, it works
function hook_link_alter(&$links, $node, $comment = NULL){
$weight = array();
foreach ($links as $key => $link) {
// assigning the weight to links
if ($key == 'comments') {
$links[$key]['weight'] = -2;
} else if ( $key == 'comment'){
$links[$key]['weight'] = -1;
} else {
// rest of the links will have weight 0
$links[$key]['weight'] = 0;
}
// an array to use in array_multisort
$weight[$key] = $links[$key]['weight'];
}
// this will sort the $links based on weight
array_multisort($weight, SORT_ASC, $links);
}
Module's weight and then module's name
Without a specific 'weight' key I think the links will be sorted by default by the module weight and then the name of the module.
Again not yet tested but if that is true it should be documented.
This function has been
This function has been removed in Drupal 7, in this version links can be added using the hook_node_view() function:
function hook_node_view($node, $view_mode){
return $node->content['links']['<group>'] = array(
'#links' => array(
array(
'title' => 'Nilsson B.V.'
'href' => 'http://www.nilsson.nl',
),
),
);
}
Thanks
Thank for your post markwittens. It works for me in drupal 7.