3

How can I access the nid of the current page so that a user can submit their comment and be returned back to the page they just commented from. Here's what I am proposing...

function mymodule_form_comment_form_alter(&$form, &$form_state) {
  $nid = arg(2);
  $form_state['redirect'] = 'mymodule/' . $nid;
}

...to be placed in template.php. However using $node -> nid (referenced in comment.module) or $nid (referenced in my custom module) throws an undefined error. How can I access the nid to complete my redirecting URL? Or is there a better way of acheiving this?

I believe this is the part of comment.module that I need to hook into and modify - so in a wider sense my question is about hooking into a core module and modifying its function:

/**
 * Process comment form submissions; prepare the comment, store it, and set a redirection target.
 */
function comment_form_submit($form, &$form_state) {
  $node = node_load($form_state['values']['nid']);
  $comment = comment_form_submit_build_comment($form, $form_state);
  if (user_access('post comments') && (user_access('administer comments') || $node->comment == COMMENT_NODE_OPEN)) {
    // Save the anonymous user information to a cookie for reuse.
    if (user_is_anonymous()) {
      user_cookie_save(array_intersect_key($form_state['values'], array_flip(array('name', 'mail', 'homepage'))));
    }

    comment_save($comment);
    $form_state['values']['cid'] = $comment->cid;

    // Add an entry to the watchdog log.
    watchdog('content', 'Comment posted: %subject.', array('%subject' => $comment->subject), WATCHDOG_NOTICE, l(t('view'), 'comment/' . $comment->cid, array('fragment' => 'comment-' . $comment->cid)));

    // Explain the approval queue if necessary.
    if ($comment->status == COMMENT_NOT_PUBLISHED) {
      if (!user_access('administer comments')) {
        drupal_set_message(t('Your comment has been queued for review by site administrators and will be published after approval.'));
      }
    }
    else {
      drupal_set_message(t('Your comment has been posted.'));
    }
    $query = array();
    // Find the current display page for this comment.
    $page = comment_get_display_page($comment->cid, $node->type);
    if ($page > 0) {
      $query['page'] = $page;
    }
    // Redirect to the newly posted comment.
    $redirect = array('node/' . $node->nid, array('query' => $query, 'fragment' => 'comment-' . $comment->cid));
  }
  else {
    watchdog('content', 'Comment: unauthorized comment submitted or comment submitted to a closed post %subject.', array('%subject' => $comment->subject), WATCHDOG_WARNING);
    drupal_set_message(t('Comment: unauthorized comment submitted or comment submitted to a closed post %subject.', array('%subject' => $comment->subject)), 'error');
    // Redirect the user to the node they are commenting on.
    $redirect = 'node/' . $node->nid;
  }
  $form_state['redirect'] = $redirect;
  // Clear the block and page caches so that anonymous users see the comment
  // they have posted.
  cache_clear_all();
}

Thanks.

5
  • Have you tried using the arg() function?
    – Alex Gill
    May 7, 2013 at 15:27
  • Thanks, but that didn't seem to work. Perhaps, I could install Token, create an action (redirect to URL), assign to a trigger (when new comment saved) and use the token [content:nid] to provide the node id, but it seems like a lot of overhead for just one redirect on one form.
    – dbj44
    May 7, 2013 at 21:30
  • 1
    If you're on a node, $nid = arg(1);. What do you mean when you say "that didn't seem to work"? May 9, 2013 at 19:38
  • I updated the hook I'm using in template.php above to reflect my attempt to use arg(). The path is mywebsite/mymodule/1. I've tried arg(1) arg(2) and arg(3). But instead of getting redirected to mymodule/1 it goes to node/1#comment-1. I'm not even sure that I'm using the right hook. You can certainly modify the comment form with that hook, but can you modify the submit handler with it?
    – dbj44
    May 10, 2013 at 11:40
  • Sounds like you are using some sort of custom comment page? Can you give more information on that?
    – rooby
    May 11, 2013 at 8:02

3 Answers 3

10
+50

To redirect a form after submission you would do this:

/**
 * Implements hook_form_FORM_ID_alter().
 */
function MYMODULE_form_comment_form_alter(&$form, &$form_state) {
  // Add a custom submit handler to act when the form submits.
  $form['#submit'][] = 'MYMODULE_custom_comment_submit_handler';
}

/**
 * Custom submit handler for the comment form.
 */
function MYMODULE_custom_comment_submit_handler($form, &$form_state) {
  // Redirect the user after submission
  $nid = arg(2);
  $form_state['redirect'] = 'mymodule/' . $nid;
}
2
  • Thanks. Both functions to be placed in template.php? or in my custom module? - which, by the way, implements the rendering of comments and the comments form by returning comment_node_page_additions($node) within a renderable array.
    – dbj44
    May 13, 2013 at 14:43
  • In a custom module for those. - And it should still work fine with your custom calling of comment_node_page_additions(). I would put it in the same custom module as it directly relates to the other code you have in there.
    – rooby
    May 13, 2013 at 23:54
4

What about using the Modules Actions, Trigger and Tokens?

Create an Action "Redirect to URL" on yoursite.com/admin/config/system/actions: Give it a name and an target URL like "node/[comment:node:nid]".

Assign that Action to the Trigger "AFTER SAVING A NEW COMMENT" on yoursite.com/admin/structure/trigger/comment

Just my thoughts...

0

Do You tried to use Rules module? I always use this if I have to do a custom redirects.

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.