How can I add a custom CSS class to checkboxes labels? I'm using Drupal 8 with the Form API and the AJAX API, now I want to add a custom CSS class to every checkbox label, because I need to use the bootstrap custom checkboxes.

PHP (slimmed):

$form['colors'] = array(
    '#type' =&gt; 'checkboxes',
    '#options' =&gt; array('1' =&gt; $this-&gt;t('Black'), '2' =&gt; $this-&gt;t('White')),
    '#attributes' =&gt; [
        'class' =&gt; ['custom-control-input'],
    ],
    '#prefix' =&gt; '&lt;div class="foobar"&gt;',
    '#suffix' =&gt; '&lt;/div&gt;'
);


HTML Result (please notice every label has the class "option"):

&lt;div class="custom-control custom-checkbox"&gt;
  &lt;div class="foobar"&gt;
    &lt;div id="edit-colors" class="form-checkboxes"&gt;
      &lt;div class="js-form-item form-item js-form-type-checkbox form-item-colors-1 js-form-item-colors-1"&gt;
        &lt;input
          class="custom-control-input form-checkbox"
          data-drupal-selector="edit-colors-1"
          data-msg-required="Black is required."
          type="checkbox"
          id="edit-colors-1"
          name="colors[1]"
          value="1"
        /&gt;
        &lt;label for="edit-colors-1" class="option"&gt;Black&lt;/label&gt;
      &lt;/div&gt;
      &lt;div class="js-form-item form-item js-form-type-checkbox form-item-colors-2 js-form-item-colors-2"&gt;
        &lt;input
          class="custom-control-input form-checkbox"
          data-drupal-selector="edit-colors-2"
          data-msg-required="White is required."
          type="checkbox"
          id="edit-colors-2"
          name="colors[2]"
          value="2"
        /&gt;
        &lt;label for="edit-colors-2" class="option"&gt;White&lt;/label&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;



My Twig Code:

&lt;div class="custom-control custom-checkbox"&gt;
 {{form.colors}}
&lt;/div&gt;


I know about the "template_preprocess_form_element" function, but I had no luck.

Do you guys have any ideas or tipps? Is there a simple way to modify the label class, or should I try the "Form Options Attributes" Plugin?

Thanks
According to issue #2774255 and line 38 in the FormElementLabelTest.php you should be able to use #label_attributes for this.


'#label_attributes' =&gt; ['class' =&gt; ['meow']],





Update: Seems you can also try to do this by implementing template_preprocess_form_element. There's an example on line 493 in the form.inc. In your theme implement template_preprocess_form_element and hopefully you find some unique identifiers in there to have this class added only for elements in certain forms or elements of certain types. And then it's

$variables['label']['#attributes'] =&gt; ['class' =&gt; ['meow']]




Last but not least there's the new Formdazzle! module you could try to use to override the label element templates directly.


  Drupal form theming with less pain
  
  Theming Drupal forms can be difficult and time-consuming. This module
  provides a set of utilities that make form theming easier.
  
  Currently, this module provides theme suggestions for forms that are
  much more useful than those provided by Drupal core.
  
  
  Theme suggestions for all form elements (including buttons)
  Theme suggestions for all form element labels
  All theme suggestions include the form ID and the form element name; e.g. [element-type]--[form-id]--[form-element-name].html.twig