Drupal 8 as a Static Site: Disable Core Search

Submitted by nigel on Sunday 18th November 2018

Since the static site will not have access to the Drupal 8 Core Search facility, it can be removed by using the command drush pm_uninstall search on the command line, or by visiting admin/modules/uninstall.

The problem with that approach is core search also has the search form that appears as a block in the navbar on every page of my website. If I uninstall core search I lose the form at the same time. It is my desire to keep on using the search form despite having an Elasticsearch backend. The proposed Elasticsearch JavaScript client will be installed on the /search route which is where the core search form action is directed to. 

Okay, so feasibly I could uninstall the core search and do either:

  1. Create a custom block with a search form that replicates (from an HTML frontend perspective) the core search form.
  2. Copy the generated markup from the core search form, and paste it into the page template.

I don't like either approach. I would prefer to keep the core search and stub it so it doesn't actually do any backend searching. That way I will have near identical functionality for both my sandbox static and sandbox Drupal sites. 

To stub the search I would have to alter the default search route and replace it with my own custom route with a controller that returns nothing.

Define the route subscriber config
The first step is to add to our custom module's services file.
  <-- previous entry snipped -->
      class: Drupal\badzilla_static\Routing\RouteSubscriber
        - { name: event_subscriber }
We have now defined where our route subscriber lives. Let's create.
Route subscriber class
We need to create the directory structure and the file itself first.
$ mkdir  src/Routing
$ touch src/Routing/RouteSubscriber.php
And the file contents are:


* @file
* Contains \Drupal\mymodule\Routing\RouteSubscriber.

namespace Drupal\badzilla_static\Routing;


* Listens to the dynamic route events.
class RouteSubscriber extends RouteSubscriberBase {

    * {@inheritdoc}
public function alterRoutes(RouteCollection $collection)
// Replace dynamically created "search.view_node_search" route's Controller with my own.
if ($route $collection->get('search.view_node_search')) {
Here I am replacing the built-in search module's controller with my own. Now let's build out my own do-nothing controller.
Custom search controller
As previously, create the file structure.
$ mkdir src/Controller
$ touch src/Controller/BadzillaStaticSearchController.php
Here's the controller in all its glory.
namespace Drupal\badzilla_static\Controller;


 * Override the Route controller for search.
class BadzillaStaticSearchController extends SearchController {

     * {@inheritdoc}
public function view(Request $requestSearchPageInterface $entity)
$build = [];
$plugin $entity->getPlugin();
$build['#title'] = $plugin->suggestedTitle();

$results = [];
$build['search_results'] = [
'#theme' => [
'item_list__search_results__' $plugin->getPluginId(),
'#items' => $results,
'#empty' => [
'#markup' => '',
'#list_type' => 'ol',
'#context' => [
'plugin' => $plugin->getPluginId(),

The view function must return a render array, and so I have honoured the convention of the parent SearchController::view to large extent although obviously in my case there will always be no results returned. A useful stub!
Clear Rebuild and Test
Since we've added to our services yaml file we need to do the customary cache rebuild for our changes to take effect.
$ drush cr
 [success] Cache rebuild complete.
Next let's attempt a search on the dynamic and the static version of my sandbox Badzilla sites. The two screenshots above show that both the static and the dynamic version of the sandbox sites are correctly showing a blank search page. Next we have to plug in the client....