Apr 03, 2017 in Snippets, Drupal 8

If you like to use the search api or a custom Views page for your search result I personally like to use the default search block and redirect it to my results page. In the example below, I redirect my search form to '/find'.

Note: it will keep the querystrings you enter. F.e. if I type in 'X' it will redirect me to /find?key=x. I can use the 'key' parameter for my identifier in my exposed filters in Views.

/*
 * Redirect global search to custom result page
 */
function MYMODULE_helper_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
    if($form_id == 'search_block_form') {
        $form['#action'] = '/find';
    }
}