Sparky
Sparky

Reputation: 4879

Add css property for individual components in form elements - Drupal

I have a drupal form and I need to add separate classes to the components in a form element.

That is I need to add the attribute class='text' to the 'textbox (the actual box)' and class='title' to the 'textbox title' (#title').

How do I do this?

Upvotes: 2

Views: 17055

Answers (2)

Nikit
Nikit

Reputation: 5128

Someone already answer to your question Remove class=“form-item” from Drupal forms.
That function give possibility add class='title' to the title.
But for class='text' you should theme_textfield (or theme_textarea) to own template.php
Take codes from ./included/form.inc
But i recommend to do theming required custom form, not all form elements. Somewhere it can raise styling conflicts etc. Of course, if client doesn't demand change all form elements. In real case changing default elements doesn't need.

Upvotes: 2

user113292
user113292

Reputation:

For the first case—adding the class to the textfield itself—use the #attributes property:

$form['foo'] = array(
  '#type' => 'textfield',
  '#title' => t('Textbox title'),
  '#attributes' => array(
    'class' => array('text'), // change to just 'text' for Drupal 6
  ),
);

For the second case——adding the class to the label—you are out of luck with the Forms API. Instead, you could target it using the wrapper class for the field and label:

.form-item-field-foo label {
  /* CSS here */
}

Upvotes: 9

Related Questions