-2

I have created a form with a label and input fields. I have an abc.theme file.

I tried using the following code.

function abc_preprocess_label(&$variables) {
   $element = $variables['element'];
   $variables['attributes']['class'][] = 'test';
}

How do I apply a class to the form label?

But seems not working

0

The hook to preprocess is form_element_label. This is a working version:

function abc_preprocess_form_element_label(&$variables) {
  $attributes = $variables['attributes'];
  $attributes->addClass('class_name');
}

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.