Redirect drupal comment submissions to the page the user commented on

Recently, a client who displays most of his content through views asked me to address an issue with comment submission. This Drupal 6 site made heavy use of views in order to render nodes in various states. Users were being confused when they submitted a form on a page with a buy button but after they had posted their comment, they were sent to a nearly identical page which did not have a buy button. After trying to find other folks who had addressed the same challenge and not seeing anything that fit, I moved onto the Drupal 6 API docs.

After reading a note on the hook_form_alter page detailing how to create a hook_form_alter function for one specific form, I added the following code to a custom module.

function comment_form_comment_form_alter(&$form, $form_state) {
unset($form['preview']);
$form['submit'] = array('#type' => 'submit', '#value' => t('Submit'), '#weight' => 20);
$form['submission_path'] = array(
'#type' => 'hidden',
'#default_value' => $_GET['q'],
'#weight' => -100,
);
$form['#redirect'] = array($form['#parameters'][1]['post']['submission_path'],'comment');
}

The odd name for the this function is due to the hook_form_FORM_ID_alter() syntax so the first comment_form is in place of the hook_form and the second for the form_id

The approach was to make it so the user was sent back to the page they submitted their comment. This meant that comments would work no matter which views driven version of the node the user was on.

To achieve this I added a hidden field to store the path that the user was on when they made their comment

$form['submission_path'] = array(
'#type' => 'hidden',
'#default_value' => $_GET['q'],
'#weight' => -100,
);

The next line is used after submission to redirect to prior page. One all comment pages I have a named anchor for #comment so the page would load at the comment part of the page instead of at the top.
$form['#redirect'] = array($form['#parameters'][1]['post']['submission_path'],'comment');

The line below was added because the client did not want users to have to preview their comment prior to posting.

unset($form['preview']);

We instead added the submit button below to allow comments to be made with only one button click.

$form['submit'] = array('#type' => 'submit', '#value' => t('Submit'), '#weight' => 20);

I hope you find this useful in your Drupal development,

Shannon Garrahy

Shannon Garrahy

Shannon Garrahy

Shannon brings a broad base of experience in deploying technology to transform business practices and creating systems that realize new opportunities. Classically trained in software development using C, Java, Delphi and others, Shannon taught himself PHP and typically leverages agile methodologies in his projects.

5 Responses

  1. Hi Shannon,
    Thanks for posting this solution. I have a site which pulls heavily on views to display comments. There’s a blog page with multiple blog entries. After each blog is a comment block displaying the most recent comments, and a link to add a comment. So our form is on another page.

    But we want to come back to the blog page after submitting this form. At the moment we are using a session variable in a php custom field to store the originating url.

    Can you think of any neater way to do this?

  2. Hi Nick,

    You should be able to adapt this sort of approach to your situation.

    $form[‘submission_path’] = array(
    ‘#type’ => ‘hidden’,
    ‘#default_value’ => $_GET[‘q’],
    ‘#weight’ => -100,
    );

    This line pulls in the current address for the purposes of redirecting back once the comment has been posted. Changing this to value you are currently storing in the session should allow you to keep the session just for session related information.

Leave a Reply

Your email address will not be published. Required fields are marked *

Categories

Search

Recent Posts

Tags