0

I have enabled comments for a custom node type.

I am trying to theme that comment form using hook_form_alter(&$form, &$form_state) (For example, I'd like to remove the label of the comment_body form field)

To do that, I use the following function:

function MYTHEME_form_comment_form_alter(&$form, &$form_state) {
    $form['comment_body']['und']['#title'][0] = '';
}

However in the output the title of the form field is still there:

actual output

Here's the markup:

enter image description here

The value of $form does not correspond to what I am used to see. Here's a screenshot from my debugger:

enter image description here

9
  • Why [LANGUAGE_NONE] in your code, and yet you do not expand ['und'] in the debugger? I mean, if that's the part that matters, why not to show it to us?
    – Mołot
    Mar 4, 2014 at 7:56
  • You show "MYTHEME" as hook in your function name, you do that in template.php? You should try to do that in a custom module. Mar 4, 2014 at 8:42
  • hook_form_BASEID_form_alter in theme is almost equivalent with module, it just gets called after the module hooks.
    – user20301
    Mar 4, 2014 at 9:42
  • @Molot: Good point. I updated the screenshot. Mar 4, 2014 at 11:48
  • OK now seems you are trying to update what should be updated. Is your hook fired at all?
    – Mołot
    Mar 4, 2014 at 11:55

2 Answers 2

0

It looks like the label "Comment*" is not from comment_body container wrapper, then again comment_body shouldnt be of type container.

Most likely the actual comment field is somewhere not visible of your debug screenshot. This is because drupal core comment module form doesnt use normal form api structure where containers only reference children and reverse. Instead the actual field is placed directly to container. Luckily drupal_render has no problem with neither structure.

Removing the label should not require specifying language on the actual field. It should be as easy as

$form['comment_body']['#title']='';

Instead with funky comment form render array you need to specify language of the containers sub items, hence the path transforms to

$form['comment_body'][LANGUAGE_NONE][0]['#title']=''

3
  • As you can see in his debugger screenshot, there is no $form['comment_body']['#title'] present, so setting it up hardly can help.
    – Mołot
    Mar 4, 2014 at 8:36
  • The example describes method usable if the comment_body would actually be a normal field. In this case the actual field most likely exists at $form['comment_body'][LANGUAGE_NONE][0]['#title'] = '';
    – user20301
    Mar 4, 2014 at 9:17
  • @user20301: No idea why your response was downvoted. I consider it quite helpful. In any case I noticed that the reference to the field was incorrect. I tried your approach ($form['comment_body'][LANGUAGE_NONE][0]['#title']='') but the Label Comment* still appears. It seems that I'll have to go deeper and debug drupal_render to see what's happening. Or hide the label via CSS (which feels hackish) Mar 4, 2014 at 11:54
0

I Was attempting to add a placeholder for the comment_body field and after struggling through this a bit, was able to figure out that the changes will take in affect if you utilize the "value" array key like so:

$form['comment_body'][LANGUAGE_NONE][0]['value']['#title']='foo bar';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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