Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 329 Vote(s) - 3.61 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Add comments to Custom node module

#1
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`.

Reply

#2
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

[To see links please register here]

Reply

#3
Since you are using a custom module, you could alter the comment_form using the [form_alter][1] 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][2] 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.

[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#4
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;


Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through