10
votes

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.

3
You say the code works and then describe that it doesn't save your comment. Can you specify more clearly what works and what doesn't work? - Jauco
I edited the question, is this more clear? - Jurgo
Unless you don't refer to a specific PHP version, like PHP-5.3 (which I don't think is the case here), just tag as PHP, not PHP5. Thank you! - hakre
It'll probably be to do with this - Clive
@Clive - Thanx for the link. I tried out the link but didn't help. Probably because the link describes self created forms in a module and this does not work for the core comments form which i'm using. - Jurgo

3 Answers

3
votes

Since you are using a custom module, you could alter the comment_form using the form_alter hook for your specific cases. You can set the form to only use your modules submit function. Then in your custom submit function, you submit the comment to the comment module for saving (calling the comment_form_submit function), and then do the redirect back to the node yourself.

Something along the lines of this:

<?php
    function mymodule_form_alter(&$form,&$form_state,$form_id){
        if ($form_id == 'comment_form' && isset($form['#node']) && ($form['#node']->type == 'mynodetype')){
            $form['#submit'] = array('mymodule_comment_form_submit');
        }
    }

    function mymodule_comment_form_submit($form,&$form_state){
        module_load_include('module','comment');
        comment_form_submit($form,$form_state);
        $url = drupal_get_path_alias('node/'.$form['#node']->nid);
        header('Location: '.$url, TRUE);
        drupal_exit($url);
    }

In your template file, still build the comment form the way you are:

$info->nid = $node_good_practice->nid;
$comment['build_info']['args'][0] = $info;
$output .= render(drupal_build_form('comment_form', $comment));

This solution may seem a bit hacky, but it works.

0
votes

I guess this would be the answer?

$comment->nid = $row->nid;
$form = drupal_get_form('comment_form', $comment);
$form['#redirect'] = 'CHANGE_VIEWSPAGE_HERE?page=' . (int)$_GET['page'];
print render($form);

can't try it myself sorry. I found it at https://drupal.stackexchange.com/questions/21692/d7-comment-form-doesnt-submit

0
votes

For some reason the problem was caused by drupal_build_form and drupal_get_form after submitting a comment. If the $_POST was filled the drupal_build_form and the drupal_get_form functions redirects met to /node/node_id/#comment-17 or to /comment/reply/<node_id> So i unset the SESSION before loading the form and the problem was fixed.

So the solution from Mike only works when you unset the SESSION. But he has been verry helpfull.

So now i have:

if(isset($_POST['form_id'])) {
  $comment = new stdClass();
  $comment->nid = $good_practice_id; // Node Id the comment will attached to
  $comment->name = $user->name;
  $comment->status = 1;
  $comment->language = "und";
  $comment->subject = $_POST['subject']; 
  $comment->comment_body[$comment->language][0]['value'] = $_POST['comment_body'][$node_good_practice->language][0]['value'];
  $comment->comment_body[$comment->language][0]['format'] = 'filtered_html'; 
  comment_submit($comment);
  comment_save($comment);
  unset($_POST);
} 

$comment = new stdClass;
$comment->nid = $node_good_practice->nid;
$form = drupal_get_form('comment_form', $node_good_practice);
$form['#action'] = url('success-stories/'.$node_good_practice->good_practice_name.'/'. $comment->nid);

$output .= theme('good_practices_front_detail_page', array('oGoodPractice' => $oGoodPractice, 'aGoodPractices' => $aGoodPractices, 'aComments' => $aComments, 'oSectors' => $oSectors, 'oCountries' => $oCountries, 'links' => $aLinks));
$output .= render($form);
$output .= theme('pager', array('tags'=>array()));

return $output;