How to add unique class to each checkbox elements I am displaying checkboxes using form API in Drupal 8. For functional and theming purpose I need to add unique custom class to each checkbox element. Is there any way to add classes to each checkbox elements on wrapper/label/input level?

Here is my form API for checkbox element.

  public function buildForm(array $form, FormStateInterface $form_state, 
   $items = array()) {
    $tids = [5,8,7,9,3,1,45];
    $terms = Term::loadMultiple($tids);
    foreach($terms as $term) {
        $name = $term-&gt;getName();
        $options[$term-&gt;id()] = $term-&gt;getName();
    }
    $default= [5];

    $form['category_type'] = [
        '#type' =&gt; 'checkboxes',
        '#options' =&gt; $options,
        '#default_value' =&gt; $default,
        '#ajax' =&gt; [
            'callback' =&gt; '::searchCallback',
            'method' =&gt; 'replace',
            'effect' =&gt; 'fade',
        ],
    ];

    return $form;
 }


I have tried to theme through hook_prepeocess_HOOK() but its not rendering in final my custom class.

   function mytheme_preprocess_input(&amp;$variables) {
      $element = $variables['element'];
      if (isset($element['#id']) &amp;&amp; $element['#type'] == 'checkbox') {
        $variables['element']['#attributes']['class'][] = 'my-custom-class';
      }
    }


Any help on this will be highly appreciated. Thanks in advance
I think the Form Options Attribute module will solve this for you.  It allows you to add option attributes in the form element render array.  Below is the example from the module page.  It also supports checkboxes as well.

$form['states'] = array(
  '#type' =&gt; 'select',
  '#title' =&gt; t('States'),
  '#options' =&gt; array(
    'AL' =&gt; t('Alabama'),
    'AK' =&gt; t('Alaska'),
    'AZ' =&gt; t('Arizona'),
    'AR' =&gt; t('Arkansas'),
    // ..
    'WI' =&gt; t('Wisconsin'),
    'WY' =&gt; t('Wyoming'),
  ),
  '#options_attributes' =&gt; array(
    'AL' =&gt; array('class' =&gt; array('southeast'), 'data-bbq-meat' =&gt; 'pork'),
    'AK' =&gt; array('class' =&gt; array('non-contiguous'), 'data-bbq-meat' =&gt; 'salmon'),
    'AZ' =&gt; array('class' =&gt; array('southwest'), 'data-bbq-meat' =&gt; 'rattlesnake'),
    'AR' =&gt; array('class' =&gt; array('south'), 'data-bbq-meat' =&gt; 'beef'),
    // ...
    'WI' =&gt; array('class' =&gt; array('midwest'), 'data-bbq-meat' =&gt; 'cheese'),
    'WY' =&gt; array('class' =&gt; array('flyover'), 'data-bbq-meat' =&gt; 'bison'),
    ),
  '#attributes' =&gt; array('class' =&gt; array('states-bbq-selector')),
);