0

I wan to be able to add certain css class to all the text fields in my content edit forms. here is a picture of what i can see using Devel: enter image description here

Those arrays under the "Und" are for the fields I want to add the class. I have tried to do this with the following:

function iclub_form_article_node_form_alter (&$form, &$form_state) {
dpm($form);
$form['field_body']['und']['0']['#attributes']['class'][] ='addRecactor';

foreach ($form['field_paragraph']['und'] as $i) {
    if (is_numeric($i)) {
        echo $i .'</br>';
        $form['field_paragraph']['und'][$i]['field_myparagraph']['und']['0']['#attributes']['class'][] ='addRecactor';
        foreach ($form['field_paragraph']['und'][$i]['field_myparagraph']['und'] as $y) {
            if (is_numeric($y)){
                $form['field_paragraph']['und'][$i]['field_myparagraph']['und'][$y]['#attributes']['class'][] ='addRecactor';
            }
        }   
    }           
}

but this does not work for all the fields and I think it is not the solution as it is not robust.

How may I be able to add the class to these fields?

1

You should write a statement that first check the form element type then add class attrubutes to it, something like //in form_alter

foreach($form as $form_elem){
if($form-elem['#type']=='textfield'){
    $form[$form_elem]['#attributes']['class'][] = 'myclass';
   }
}

(this is not the exact solution,I just want give you idea)

another way is using hook_preprocess_field

and the final and easy way is using js to add class, because drupal set default class for his textfields,you just need one line jquery code to add you

   $('.form-text').addClass('myclass');

if you don't want this js not load in all page, I suggest you add your js with drupal_add_js conditionally in hook_form_alter, it means that

    //in hook_form_alter
     $des_forms=array('node_add_page','node_add_prodcut');
    if(in_array($from_id,$des_forms)){
    drupal_add_js('your js'); //you also add it inline,but I suggest you put it in js and add it as file
}

again I tell you because I have no time in this moment I just suggest you some solution idea, I hope could help you

0
$form['name']['#attributes']['class'] = array('class' => 'formInput formInputB');

Try this

  • What do you mean by 'name'? please take into consideration that the number of fields are not limited and user can add any number of them during content creation. So I need a loop which adds the class to each field. – lameei Oct 17 '14 at 13:45
  • $delta = 0; $max_delta = $form['field_set_name'][LANGUAGE_NONE]['#path_to_array']; while ($delta <= count($max_delta)) { $form['field_name']['#attributes']['class'] = array('class' => 'formInput formInputB'); $delta++; } – harsh_behl_0007 Oct 18 '14 at 4:16

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.