07-27-2023, 12:01 AM
In my custom module I would like to add the comments functionality. I've tried a couple of things but didn't workout so far.
// render comments form
$output .= theme('my_module_front_page');
$comment = new stdClass;
$comment->nid = $node_good_practice->nid;
$output .= render(drupal_get_form('comment_form', $comment));
return $output;
The code above puts the comments form to my node page.
But when I fill in the comment form and submit, it redirects me to this page: `comment/reply/node id` and then i have to fill in my comment all over again and the comment isn't saved.
I would like to submit and stay on the same page instead of the redirect. And the comment must be saved after submitting.
Right now, the comment form appears on my node page (custom module template). I enter a comment and click on "Save."
I am sent to `/comment/reply/<node_id>`, but all the comment fields are empty. The comment isn't saved either.
What I would like to happen is:
- Having a comment form on the node page
- Enter a comment
- Click on "Save"
- Drupal saves the comment, and redirect me back to the node/page I was viewing.
Things I've tried
----------
- Adding Redirect
$form['#redirect'] = "/success-stories/".$node_good_practice->good_practice_name."/".$node_good_practice->nid;
It did not change anything.
- Changing action
$form['#action'] = "/success-stories/".$node_good_practice->good_practice_name."/".$node_good_practice->nid;
It redirects me to `node/node_id/#comment-17`
- Use `drupal_build_form()`
$info->nid = $node_good_practice->nid;
$comment['build_info']['args'][0] = $info;
$comment['redirect'] = "http://www.google.nl";
$output .= render(drupal_build_form('comment_form', $comment));
The form is being displayed, but it does not redirect; it is sent to `comment/reply/node_id`.
// render comments form
$output .= theme('my_module_front_page');
$comment = new stdClass;
$comment->nid = $node_good_practice->nid;
$output .= render(drupal_get_form('comment_form', $comment));
return $output;
The code above puts the comments form to my node page.
But when I fill in the comment form and submit, it redirects me to this page: `comment/reply/node id` and then i have to fill in my comment all over again and the comment isn't saved.
I would like to submit and stay on the same page instead of the redirect. And the comment must be saved after submitting.
Right now, the comment form appears on my node page (custom module template). I enter a comment and click on "Save."
I am sent to `/comment/reply/<node_id>`, but all the comment fields are empty. The comment isn't saved either.
What I would like to happen is:
- Having a comment form on the node page
- Enter a comment
- Click on "Save"
- Drupal saves the comment, and redirect me back to the node/page I was viewing.
Things I've tried
----------
- Adding Redirect
$form['#redirect'] = "/success-stories/".$node_good_practice->good_practice_name."/".$node_good_practice->nid;
It did not change anything.
- Changing action
$form['#action'] = "/success-stories/".$node_good_practice->good_practice_name."/".$node_good_practice->nid;
It redirects me to `node/node_id/#comment-17`
- Use `drupal_build_form()`
$info->nid = $node_good_practice->nid;
$comment['build_info']['args'][0] = $info;
$comment['redirect'] = "http://www.google.nl";
$output .= render(drupal_build_form('comment_form', $comment));
The form is being displayed, but it does not redirect; it is sent to `comment/reply/node_id`.