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
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
Categories
Search
Recent Posts
Accella and NVAR Earn Prestigious 2026 ASAE Gold Circle Award for AI-Powered Member App
AEO Webinar Series Part Three Recap: The Impact of AEO On Your Member Acquisition Funnel
[WEBINAR REPLAY] DEATH TO PDFs: From Static Documents to Discoverable Knowledge
2026 Isn’t About Chasing AI, It’s About Fixing Your Content Foundation
AEO Webinar Series Part Two Recap: How AI Is Reshaping Search and Your Association’s Visibility
Key Takeaways from digitalNow 2025: How AI Is Shaping the Future of Associations
Tags