Drupal 8 hook_form_alter not called in the search block form

1.4k views Asked by At

I am trying to modify the form action of the search block in my Drupal 8 project. The search block is placed in the Primary menu area. I added the following code in my .theme file

function mytheme_form_alter(&$form, FormStateInterface $form_state, $form_id) {
echo 'my search form'.$form_id; 
}

This form alter is not called when the page is loaded.

I tried adding form_alter in my custom module. It is also not working. Any ideas?

2

There are 2 answers

0
Pramod Sivadas On

I found the answer. We can change the form action in block--search.html.twig file in the theme folder.

0
Denis Viunyk On

You cannot use hook_form_alter in the theme, only in the modules