0
function template_preprocess_bef_radios(&$variables) {
  $variables['children'] = Element::children($variables['element']);
  _bef_preprocess_nested_elements($variables);
    foreach($variables['element'] as $tid => $value) {
        if (is_numeric($tid) || $tid === 'All') {
            $value['#attributes']['class'][] = Html::cleanCssIdentifier($value['#title']);
        }
    }
 }

While trying the above does not add class to radio button under radios(which has multiple options) How can I add class to the options ?

1

You are almost there just you should change $variables['element'][$tid]['#attributes']['class'] instead of $value['#attributes']['class'] try with:

/**
 * Implements hook_preprocess_HOOK().
 */
function template_preprocess_bef_radios(&$variables) {
  // No need to this two lines 
//  $variables['children'] = Element::children($variables['element']);
//  _bef_preprocess_nested_elements($variables);
  foreach($variables['element'] as $tid => $value) {
    if (is_numeric($tid) || $tid === 'All') {
      $variables['element'][$tid]['#attributes']['class'][] = Html::cleanCssIdentifier($value['#title']);
    }
  }
}

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.