The question is simple , in my theme I want to apply an attribute to all textboxes in whole forms . I know how to add an attribute to an specific element in drupal using hook_form_alter , but it is too time consuming for lots of elements , so I'm eager to see if there is an easier way to do that.Any suggestion?
2 Answers
If you don't want to use form_alter type of hooks, I would do it like this:
<?php
function somemodule_preprocess_textfield(&$variables) {
$variables['element']['#attributes']['custom_attr'] = 'somevalue';
}
You can replace _textfield with _textarea, I don't know what you mean by textbox :-)
Hope this helps!
You'll want to use hook_field_widget_WIDGET_TYPE_form_alter() for this. The following will add the "my-class" class to all textfields:
/**
* Implements hook_field_widget_WIDGET_TYPE_form_alter().
*/
function test_field_widget_text_textfield_form_alter(&$element, &$form_state, $context) {
$element['value']['#attributes']['class'][] = 'my-class';
}