#StackBounty: #views #8 #theming Add class to form-tag in in views exposed form via template_preprocess_views_exposed_form()

Bounty: 50

I wonder how I can add a class to the form tag (of the search_content view’s) exposed form using template_preprocess_views_exposed_form() (or even better template_preprocess_views_exposed_form__search_content()).

It works so far using the following code in MY_THEME.theme:

function MY_THEME_preprocess_form(&$variables) {
  if ($variables['element']['#form_id'] == 'views_exposed_form' && $variables['element']['#view_id'] == 'search_content') {
    $variables['attributes']['class'][] = 'row';
  }
}

But I can’t get it work for:

function MY_THEME_preprocess_views_exposed_form__search_content(&$variables) {
  // I tried:
  // $variables['attributes']['class'][] = 'does-not-work';

  // and also:
  // $variables['attributes'] = new Attribute($variables['attributes']);
  // $variables['attributes']->addClass('does-not-work');

  // and also manipulating $variables['form'], but this didn't work either.
}

The function gets called, but it’s not adding the class to the form tag. Is it possible to get it work with template_preprocess_views_exposed_form()?

Screenshot of kint($variables) output, for each function:

enter image description here


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.