Drupal - How to add a button or link to perform an action on a node with node_view?

You can implement the hook_node_view() like the following code:

function YOUR_MODULE_node_view($node, $view_mode, $langcode) {
  if ($node->type == 'CONTENT_TYPE_NAME') {
    $form = drupal_get_form('redirect_button_form');
    $node->content['redirect_button'] = $form;
 }
}

function redirect_button_form($form, &$form_submit) {
    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('New Item'),
    );
    return $form;
}

function redirect_button_form_submit($form, &$form_state) {
    //here the path where you need to go
    $path = 'node/add/CONTENT_TYPE_NAME';
    drupal_goto($path);
}

For adding any custom link/button on node display, try by adding your code in tpl file. For customizing node display add your code in node.tpl.php.

For tpl naming conventions and more details, Please refer

  1. Theming nodes by content type.
  2. Drupal 7 Template (Theme Hook) Suggestions

Hope this will help you.

Tags:

Entities

7