2

I want a value to be automatically added to the title text-field in the comment form ; this value would be indeed the current node title.

I've started working on a hook_form_alter() (added in a custom module already installed and enabled) like this one :

   function ta_module_form_alter(&$form, &$form_state, $form_id) {
   if ($form_id == 'comment-form') {
        $form['comment']['title']['#value'] = 'Blablabla';
    }
   }

But as you might guess it's not working... I've got several issues:

  • Is the $form_id the right one ?

  • I didn't manage to successfully use print_r($form) (or it returned nothing) so I guessed the path to the title value. Might be incorrect too...

  • And finally, I really don't know how to get the current node title variable, so as to attribute it the title value!

Thanks for reading, I hope I've been understandable ! ;)

2

You're right, you most likely have the $form_id wrong. To check this you can use drupal_set_message($form_id); and refresh the page to see all form ids on the current page.

Once you have the form id correct, then you can use print_r($form);

For example, I got this working on the comment form on my node type "Announcement":

function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'comment_node_announcement_form') {
    $form['subject']['#default_value'] = $form['#node']->title;
    //print_r($form);
  }
  //drupal_set_message($form_id); // Use to get $form_id
}
  • Thks for the drupal_set_message($form_id) trick, very useful ! The rest of the code you gave works perfectly. The only crappy thing is that I'll have to add an "OR comment_node_TYPE_form" in the IF condition for each content-type I want to make it... But, that works at least ! ^^ – nyoz Jul 29 '11 at 20:29

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.