I have ported an old site to Drupal 7. It contains dynamic pages generated from database records. For practical reasons these pages cannot be converted to Drupal nodes but I do require comments on them. I followed the idea in this post and wrote some code. Basically for each page I create an empty node, append it to my page, so the comment attached to the empty node appears to be attached to my non-node page. I have the following code in a callback function:

<?php
 
// in my callback function
 
$output = '......' // my page content

  // find the empty drupal node attached to my page (I create a db table to store these relationships)
 
$drupal_nid = ......

  if (
module_exists('comment') && user_access('access comments') && variable_get ('comment_api', COMMENT_NODE_OPEN) != COMMENT_NODE_HIDDEN) {
   
$comment_form = comment_node_page_additions(node_load($drupal_nid));
   
$output .= drupal_render($comment_form);
}
  return
$output;
?>

The code works and I can see the comment form on my pages now. I can also actually write and submit comments. There are two problems I need to fix:

First, after submitting a comment, the system redirects to a URL like http://www.example.com/comment/5#comment-5 (that is the empty node I created), I need to redirect to the associated content page instead.

Second, when I go back to the content page, I can see the comment I just added. But there is a 'Permalink' also pointing to the empty node.

I understand I need to create some sort of "...form_alter" function to change the behaviour of comment submission but I am not sure exactly which function to create and which variables to overwrite. Can anyone point me the right direction? Thanks for your patience to read this rather long post.

Comments

Still struggling to find a

Still struggling to find a solution. Anyone can advise? Thanks.

1) Implement

1) Implement hook_form_alter()
2) Check the form_id and only act upon your specific form
3) Add your own submit function to the #submit array for the form
4) In your submit function set $form_state['redirect'] to point at the node.

Or, you may be able to set $form_state['redirect'] or $form['redirect'] in hook_form_alter() without even doing steps 2-4 above, though I can't recall off the top of my head if either of these will work.

Jaypan We build websites

Thanks for your advice. I got

Thanks for your advice. I got it done! Here are some key points:

Recall that I need to add comment form to some pages that are not Drupal nodes. So I create a content type called empty_node containing nothing so that I can append them below my page content. By default after comment submission the system redirect to the node where the comment is associated to. In my case, that means the system redirect to an empty page with only the comments. So I need to somehow change the redirection behavior. Here is what I have to do:

<?php
function mymodule_form_alter(&$form, &$form_state, $form_id) {
 
// find which comment form to modify, can find this out by drupal_set_message($form_id);
 
if ($form_id=='comment_node_empty_node_form') {
   
// here I need to find out which page the empty node is attached to and generate its url from my business logic
   
$redirect_url = '......';

   
// there are a number of ways to redirect, refer to <a href="http://drupal.org/node/1074616
" title="http://drupal.org/node/1074616
" rel="nofollow">http://drupal.org/node/1074616
</a>    // I choose to append a destination to the form's action URL
    // one can also create a submit handler function as pointed out by Jaypan above
   
$form['#action'] .= '?destination='. $redirect_url;
  }
}
?>

Then I need to clean up a few small things:

- a permalink to the comment are displayed in my theme and it points to the empty node. I remove this from the theme template because I do not need this.
- a word 'new' is displayed above the title of the comment. This is suppose to be shown only after the comment submission, but somehow remains there afterwards. So I edit "comment.tpl.php" in my theme to remove it.
- The 'reply' link after each existing comment points to the empty node but I find this acceptable because the comment to be replied is shown above the reply form, no confusion there.

nobody click here