Same name and namespace in other branches
  1. 4.6.x developer/hooks/core.php \hook_link()
  2. 4.7.x developer/hooks/core.php \hook_link()
  3. 5.x developer/hooks/core.php \hook_link()

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

14 functions implement hook_link()

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

blog_link in modules/blog/blog.module
Implementation of hook_link().
book_link in modules/book/book.module
Implementation of hook_link().
comment_link in modules/comment/comment.module
Implementation of hook_link().
drupal_add_link in includes/common.inc
Add a <link> tag to the page's HEAD.
node_link in modules/node/node.module
Implementation of hook_link().

... See full list

5 invocations of hook_link()
comment_render in modules/comment/comment.module
Renders comment(s).
node_view in modules/node/node.module
Generate a display of the given node.
poll_view in modules/poll/poll.module
Implementation of hook_view().
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 990
These are the hooks that are invoked by the Drupal core.

Code

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;
}