0

I am using Drupal 7 and I need to change classes for ALL input textfield wrappers (class='form-item'). How can I do this without JS? please help

I could change form's, form-submit's and submit-wrapper's (form-actions) classes with this code in template.php:

    function mytheme_form_alter(&$form, &$form_state, $form_id) {
        $form['actions']['submit']['#attributes'] = array('class' => array('my_class', 'my_own_class')); 
    }

but I cant do anything with textfields and their wrappers

  • By looping over all $form items and checking if their #type is of textfield, you should be able to achieve this in your alter hook: foreach ($form as $key => $element) { if ($element['#type'] == 'textarea') { $form[$key]['#attributes'][] = 'my-attr'; } } – Neograph734 Jul 26 '17 at 11:43
0

You can override the output of theme_form_element() function in your theme's template.php file. Just copy the function from includes/form.inc file and put it in template.php as MYTHEME_form_element(), and you can now change classes for only text fields with something like this before initializing $output variable :

if($element['#type'] == 'textfield'){
  // Add a class specific to textfields
  $attributes['class'][] = 'custom-textfield-class';
}

This code is not tested, but it should work. You should clear drupal cache after implementing the function in template.php.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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