4

How do I add the comments form (of the comments module) programmatically in a custom-made module?
In other words, what proper code should be added to your module code to add a complete "Post New Comment" form (of the Comments core module)?

That said, how do I force the installation of the Comment module with the installation of your own module?


I'm re-asking my question in an edited version with some new results i've reached :).

I've created a custom menu in a custom module of mine, with items of course.
I wanted to add the comment form (of the Comment module) to the page that my item takes me to.
The problem is that the page i'm redirected to is not a node, but it is a render-able page that displays the data according to it's 'page_arguments'.
It is as follows:


$items['car/%'] = array(
        'title' => 'Cars',
        'page callback' => 'car_page',
        'page arguments' => array(1),
        'access callback' => TRUE,
        'type' => MENU_CALLBACK,
);

I want to attach the comment form (of the Comment Module) to that page.
I used the following code:


module_load_include('module', 'contact');

$build = array(
    'header_text' => array(
        '#type' => 'markup',
        '#markup' => 'Building comments in page.',
    ),
    'the_form' => drupal_get_form('comment_form', ''),
);

return $build;

All is correct code.

But the problem is that the comment form and the posted comments are loaded depending on the node Id (nid), and in my case there is no node Id (nid)!

How could i assign a specific identifier of that custom page so that the comment form functions correctly and comments related to that page are correctly loaded?!

And in the code above, i managed to call the form using drupal_get_form(), and assigned an empty second argument! I don't think that's right, right?

Any ideas!?

| improve this question | | | | |
3

comment_form() is expecting the parameter to be an object rather than an array so you should be doing something like this:

$comment = new stdClass;
$comment->nid = $row->nid;
$form = drupal_get_form('comment_form', $comment);

Note the lack of a 3rd parameter passed to drupal_get_form(), comment_form() only accepts the comment as a parameter, if you want to add a title you'll have to print that separately.

Finally drupal_get_form() returns a render array rather than a string so you need to pass it through the render() function:

print render($form);

The extra errors you're getting are just side-effects of passing an array rather than an object to the comment_form() function

| improve this answer | | | | |
2

Attach comments to what? Note that you can only attach comments to nodes.

The API function for saving comments is comment_save(). You will have to figure out what kind if properties you need to have on the $comment object that you want to pass in. Probably at least nid, subject and body['und'][0]['value'].

That said, how to force the installation of the comments module with the installation of your own module?

Thtat is a separate question, try to only ask a single question at once. You need to add dependencies[] = comment in your .info file.

| improve this answer | | | | |
  • Sorry for the 2 questions, but they seemed a bit related. Concerning the comments, i created a custom form which i want users to be able to commment on. So, i want to attach comments to the page that my custom form is in! Is that possible? – Peter May 29 '11 at 15:10
  • No. Not without some hacks, that is. (For example, you could create a fake node and then add comments to that node id and query them). – Berdir May 29 '11 at 15:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.