为表单元素中的各个组件添加css属性 - Drupal

Spa*_*rky 2 drupal drupal-forms

我有一个drupal表单,我需要在表单元素中的组件中添加单独的类.

那就是我需要将属性class ='text'添加到'textbox(实际框)'和class ='title'添加到'textbox title'(#title').

我该怎么做呢?

小智 9

对于第一种情况 - 将类添加到textfield本身 - 使用#attributes属性:

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

对于第二种情况 - 将类添加到标签 - 您对Forms API运气不佳.相反,您可以使用字段和标签的包装类来定位它:

.form-item-field-foo label {
  /* CSS here */
}
Run Code Online (Sandbox Code Playgroud)