hide cck labels in input forms

Events happening in the community are now at Drupal community events on www.drupal.org.
barako's picture

Hi everybody,

I have a problem with the display of labels of cck fields.
I would like to not display them in input forms.
In fact, in a form, I have a lot of CCK fields and their names are complicated like xxx_xxx_xxx so for my users, in the input form, they'll see these labels to fill their fields which it's not very nice.

For the output, there's no problem because I can theme them.

But for the input form I don't know how to do this.
I created a file tpl.php and I just wrote for example : <div id="oliv"><?php print drupal_render($form['field_contenu']); ?></div>
(I did as in the tutorial in http://drupal.org/node/98253#comment-251619).

And in my form, Drupal displays the label, the textbox and the help text.
All I want is to hide the label and I don't know how to do this.
The only solution I found was to write this code in my file style.css :

.form-item label {
  display: none;
}

But the disadvantage is that code is applied in all the forms of my website so it's not a good solution...

Could someone tell me how to do this please more properly please?

Thanks

Comments

Hope this helps you...

vainshane's picture

Try setting the #title attribute to "" -- that stops the bold labels being applied to form elements. This worked for me using select elements:

$select_input['#title'] = "";

Hope this gets you somewhere!

Cheers,

Jeremy

I'm looking for a solution too

rjdempsey's picture

I'm a bit newbish with Drupal. I too am looking to remove specific labels from CCK input forms. Jeremy, in what file exactly do I need to add that line?

Bit of a clarification

vainshane's picture

In various places in Drupal, whenever you have a form, you have a $form array which contains all the elements in that form. You can define a form array in various methods, for example the hook_admin() method (where "hook" just means "current_module_name", it's nothing too confusing :)). Anywhere you see the $form element, or anywhere you want to use a form, you'll see these types of arrays.

Each form element sits in the array as a sub-array of parameters, like this for example:

<?php
$form
['taxonomy_vocab_select'] = array(
     
'#type' => 'select',
     
'#options' => $vocab_options,
     
'#title' => 'Select vocabulary',
     
'#name' => 'vocab_select',
     
'#attributes' => array('onchange' => 'vocab(this.value);return false'),
     
'#suffix' => '<div id="div_termselect"></div>',
    );
?>

Here you can see a list of attributes, starting with #, and their values. This translates into a form input tag with type='select', name='vocab_select', onchange='vocab(this.value);return false', etc.

Now, when Drupal sees a 'title' attribute, it doesn't just put it into the input tag (<input title='Select vocabulary'>). Instead, it knows to put that above the input tag, inside <strong>tags. So you get

<strong>Select vocabulary</strong>
<input type='select' ...>

Now in your case, you want to kill off what's inside those <strong> tags so you just see the input. Hence, setting #title to ' ' in the applicable form array.

So, what do you do if your code doesn't seem to have any of these $form arrays mentioned where you need it? Try two things:

First, if you think you've got a bit of code that's happening at the right moment, for example you're already working in the hook_admin method, but you just don't see the $form -- try putting in this line: vardump($form); -- that'll tell you if $form is in scope at that method. Refresh the page and you should see all the form array data. If you get that, scroll down the list of form element arrays and figure out the name of the one you want to affect. Its name should be the same as its form element name, by convention, but it could be called anything. So figure out where it is, then set the #title attribute of that array to be empty. You can also do stuff like "unset($form['input_1'];" to completely remove an element.

Alternatively if you can't find a method in your module where you should be able to put your var_dump($form) so it'll work, implement the method hook_form_alter in your module, and just start with var_dump($form); inside it; that method is called whenever a form is displayed, so you should get some traction there. Don't forget to rename the method to my_module_name_form_alter.

Once you start working with forms, you'll need to bookmark these pages:

http://drupal.org/node/202756
http://api.drupal.org/api/file/forms_api.html/5
http://api.drupal.org/api/file/forms_api_reference.html/5

You have to read them a hundred times before they make sense fully, but they're essential.

Cheers,

Jeremy

Thank you so much!

rjdempsey's picture

Thanks so much for taking the time to fully explain. You've been a great help.

Ryan

Content Construction Kit (CCK)

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: