I can use hook_links() to add a link to my node, but I don't know how to get that link displayed in the body except by manually adding it to the node_body in hook_view(), i.e.:

$node->body .= l($link[<link name>]['href']) 

However, this doesn't handle $link properly when the format of the link is a "non-link" (i.e. w/o an href). What is the proper method for doing this?

Comments

nancydru’s picture

          $links['pinkslip_author_link'] = array(
                'title' => t('See more articles from '.$node->name),
                'href' => "pinkslip/author/$node->name",
                'attributes' => array('title' => t('See more articles from '.$node->name)),
                'html' => FALSE
              );

The link shows up at the bottom of the node.

Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database

mixersoft’s picture

hat's the output from the hook_links() function.

Here's a snippet from product.module:

function product_link($type, $node = null, $teaser = false) {
drupal_set_message(t('snhp->product_link(): $node->ptype == %a', array('%a'=>$node->ptype)),'error');
  $links = array();

  if ($type == 'node' && $node->ptype) {
    /* Determine whether or not the 'add to cart' link needs to be displayed. */
    $item = module_invoke('cart', 'get_items');
    if (variable_get('product_cart_addition_by_link', 1) && $node->hide_cart_link == 0) {

      /* Right here we need to check if a given product type is in stock */
      if (product_get_attributes($node, 'in_stock') !== false) {
      <deleted...>
        }
        else {
          $links['add_to_cart'] = array(
            'title' => t('Add to cart'),
            'href' => "cart/add/{$node->nid}",
            'attributes' => array('class' => 'cart_link', 'title' => t('Add this item to your shopping cart.')),
            'query' => variable_get('product_cart_is_destination', 0) ? NULL : drupal_get_destination()
          );
        }
      }
      else {
        $links['add_to_cart'] = array(
          'title' => t('Sold out'),
        );
      }
    }
  }
  return $links;
}

But what do you mean that it automatically shows up at the bottom of the node?

Actually, I should clarify that the place I want it to show up is in a view of the node, which is why I am messing with $node->body and $output from above. If I'm putting the link in a view, should it also automatically show up?

nancydru’s picture

Since I don't use hook_view, the standard node presentation places the links at the bottom of the display of the node. But I would think that any view presentation would include them. Take a look at how the node module does it and compare it to yours.

Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database

nevets’s picture

The links are typically printed in node.tpl.php something like this

<?php
  if ($links) {
    print $links;
  }
?>

While often after the content you can also place them before the content.

mixersoft’s picture

The output, $links = hook_links(), is an array. is this the same $links that you can simply print in node.tpl.php?

mixersoft’s picture

You have to theme the output from hook_links() to get it to display it properly:

So in hook_view() for the module in question, I just added:

        $link_array = hook_link('node', $node);
        $node->body .= theme_links($link_array);
        $output .= check_markup($node->body);

Thanks for the tip on node.tpl.php, it got me thinking in the right direction.