4

I'm trying to modify the array of content, and add an extra link.

  • I don't want to use the Link module; this link is going to be generated with other fields, it's something like Computed Field module (which is in developing stage for Drupal 7, and I'd rather not use it).
  • I don't want to just add HTML/PHP to node.tpl.php. I want this to be nice solution.

I've been trying with adding new field to $node->content. I managed to copy another field and display it once again. I was able to change label of that field, but not the link itself.

Does anyone have any idea on how to proceed?

2
  • Give us some code and we tell you where you messed it up. Alternatively, read the documentation on what drupal_render expects.
    – Oswald
    Commented Aug 30, 2011 at 10:05
  • Showing the code you are using would help to understand what it is wrong with the code you are using. Differently, who answers could just report what you need to do, which would be what you are already doing.
    – avpaderno
    Commented Aug 30, 2011 at 12:11

3 Answers 3

9

You can do this by implementing hook_node_view.

For example:

function my_module_node_view($node, $view_mode, $langcode) {
  switch ($node->type) {
    case 'my_node_type':
      $node->content['extra_link'] = array(
          '#weight' => 10,
          '#theme' => 'link',
          '#path' => 'path_to_content',
          '#text' => t('An extra link'),
          '#options' => array(
              'attributes' => array(),
              'html' => FALSE
          ),
      );
      break;
  }
}
3

Whisky's answer will work most of the time, but didn't work if you're trying to add query arguments to the end of the URL. In my case, I'm prepopulating an Entity Reference field which needs a URL that looks like this:

node/add/er-engagement?field_er_cal_entity_reference=19

Whiskey's answer doesn't seem to allow this, whether 'html' is TRUE or FALSE.

The '#markup' attribute will work, however.

function hook_node_view($node, $view_mode, $langcode){
  switch ($node->type) {
      case 'er_event':
        $node->content['links']['extra_link'] = array(
            '#weight' => 100, // place it at the bottom
            '#markup' => l('Add attendee data', 'node/add/er-engagement', array('query' => array('field_er_cal_entity_reference' => $node->nid))),
        );
        break;
}

}

That works great if you need those arguments in there. The documentation is lacking, but thanks to this post, it helps to check out Drupal 7's blog.module, just search for '#links' to get a clue.

1

To add query fragment just put it inside the option array:

$node->content['links']['extra_link'] = array(
    '#weight' => 10,
    '#theme' => 'link',
    '#path' => 'node/add/er-engagement',
    '#text' => t('Add attendee data'),
    '#options' => array(
        'html' => FALSE,
        'query' => array('field_er_cal_entity_reference' => $node->nid)
    ),
);

This will return an URL like this: node/add/er-engagement?field_er_cal_entity_reference=13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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