Strange behavior: Extra css classes are NOT merged with the 'Search Block' form tag classes but with those of BLOCK wrapping the form tag


I have tested it too, it seems not working with MYTHEME_form_FROM_ID_alter there is another way to do so.

  1. Implements hook_theme
 * Implements hook_theme().
function MyModule_theme($existing, $type, $theme, $path) {
  return [
    'search_block_form' => [
      'base hook' => 'form',
      'template'  => 'form--search-block-form',

  1. Create form--search-block-form.html.twig in templates folder of your module put in it.
{# add your inline-form class here #}
 <form{{ attributes.addClass('search-form', 'search-block-form', 'inline-form') }}>
  {{ children }}
  1. Clear cash.

It's fairly straight forward PHP mistake $form['#attributes']['class'] is an array of classes

And by doing $form['#attributes']['class'] = 'form-inline'; you're saying forget whatever $form['#attributes']['class'] was before its now a string that contains 'form-inline'. So you're changing the variable from an array to a string.

Long story short you should be adding your class to the existing array with:

 $form['#attributes']['class'][] = 'form-inline';

Note the [] after ['class']

That explains why you have no classes coming out on your form. The issue about the classes on your outer div isn't related to this code and you must have something else going on too. Perhaps a block alter hook

after digging for hours on docs, i ended up with this solution

function mytheme_preprocess_form(&$variables) {
  // kint($variables);
  if ($variables['element']['#form_id'] == 'search_block_form') {
    $variables['attributes']['class'][] = 'form-inline ';

it works as expected, but hook_form_FORM_ID_alter make more sens to me than hook_preprocess_form.

Tags: Drupal 8 / Forms / Hooks

Similar questions

If I have added extra fields to a user profile, how can I programmatically check if those fields are empty?
I have added some fields to my user entity, (doing this in hook_install(), in .install file). This is in a custom module. If I uninstall this custom module I only want to uninstall those fields if users don't have any values for the field, so I don't end up losing data from my users. How can I check fields of a user?
Strange form behaviour inserting extra HTML
We are seeing a strange issue, on page load we get extra html inserted around a form submit button. Once the button has been clicked and #element_validate has been fired then the form displays correctly with the expected html output. On page load (extra html coded added around the submit button): On element_validate (correct html): In our DEV envir...
Images are uploaded but those are not avaliable in a form
I have a problem with webform module and that too with my site only :( When I add form field with image upload, photos are uploaded but when I check submissions there are empty places, only I can find this photo in FTP in path sites/all/webform/ When I add required field, so user can not go to the next page break I choose file, format and weight is...
How to add wrapping elements & classes to a table in a view?
I'm building a custom theme using bootstrap 3, I need responsive tables. Yes, I'm aware of responsive_tables but I didn't like it. I want to use bootstrap's native .table-responsive. for that I need to wrap table like this: I need to add couple more classes to the <table> element. How to do that?
Call a drupal behavior method from inside another behavior
How do you call a drupal behavior method from inside another behavior? My example below See how I try to call exampleMethod() that belongs to behavior exampleOne inside behavior exampleTwo? I would think I can call it like that but I keep getting undefined when I call it. Though if I call it like this console.log(Drupal.behaviors.exampleOne.example...
User can input multiple terms, results can tagged as some or all of those terms, but not other terms
I think I'm going to have to make a custom operator for this, but I figured I might as well ask in case I'm missing something obvious. Let's say I have (in D7) three nodes tagged with colours: Node 1 Node 2 Node 3 Node 4 The user selects red, blue, and green from the exposed filter in a view. This view should return Node 1, Node 3, and Node 4, but ...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.