Passing taxonomy term through drupal_html_class() and use it as row class name In a view I would like to use a excluded term reference field as source for the the row classname. But the values need to be sanitized by putting the term names through drupal_html_class(), otherwise the resulting classnames are not valid in every case.

How can I sanitize the values using a preprocess function?
I got it working by using this preprocess function:

function mytheme_preprocess_views_view_field(&amp;$variables) {
  $view = $variables['view'];
  $field = $variables['field'];
  if ($view-&gt;id() == 'VIEW_NAME' &amp;&amp; $view-&gt;current_display == 'DISPLAY_NAME') {
    if ($field-&gt;field == 'field_category') {
      $tid = $variables['row']-&gt;_entity-&gt;field_category-&gt;target_id;
      $term = \Drupal::entityTypeManager()-&gt;getStorage('taxonomy_term')-&gt;load($tid);
      $name = $term-&gt;getName();
      $variables['output'] = \Drupal\Component\Utility\Html::cleanCssIdentifier($name);
    }
  }
}


An alternative would be using mytheme_views_pre_render.