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.
badzilla_static.services.yml
  <-- previous entry snipped -->
  badzilla.route_subscriber:
      class: Drupal\badzilla_static\Routing\RouteSubscriber
      tags:
        - { 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:
RouteSubscriber.php

<?php

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

namespace Drupal\badzilla_static\Routing;

use 
Drupal\Core\Routing\RouteSubscriberBase;
use 
Symfony\Component\Routing\RouteCollection;



/**
* 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')) {
            
$route->setDefault('_controller''\Drupal\badzilla_static\Controller\BadzillaStaticSearchController::view');
        }
    }
}
?>
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.
BadzillaStaticSearchController.php
<?php
namespace Drupal\badzilla_static\Controller;

use 
Drupal\search\SearchPageInterface;
use 
Symfony\Component\HttpFoundation\Request;
use 
Drupal\search\Controller\SearchController;

/**
 * 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(),
                
'item_list__search_results',
            ],
            
'#items' => $results,
            
'#empty' => [
                
'#markup' => '',
            ],
            
'#list_type' => 'ol',
            
'#context' => [
                
'plugin' => $plugin->getPluginId(),
            ],
        ];

        return 
$build;
    }
}
?>
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
Seach1
Search2
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....