I have a search box using custom_search module. I am trying to add multiple classes to the input field using a hook.

Here is the code I have tried:

function mytheme_form_search_block_form_alter(&$form, &$form_state) {
      $form['keys']['#attributes']['class'][] = 'custom_search-keys','form-search','form-control';

But it does not add the classes to the search box input field?

  • Is this the correct hook? In Drupal 8, you must pass $form_state as an instance of FormStateInterface. Can you verify the code is being hit?
    – Kevin
    Jul 2, 2017 at 22:56
  • As well as verify that the attribute key is correct, when validating that this code is hit.
    – Kevin
    Jul 2, 2017 at 23:11

1 Answer 1


Try this one, add class as array.

$form['keys']['#attributes'] = array('class' => array('custom_search-keys','form-search','form-control'));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.