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:
- Create a custom block with a search form that replicates (from an HTML frontend perspective) the core search form.
- 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.
badzilla_static.services.yml
<-- previous entry snipped --> badzilla.route_subscriber: class: Drupal\badzilla_static\Routing\RouteSubscriber tags: - { name: event_subscriber }
$ mkdir src/Routing $ touch src/Routing/RouteSubscriber.php
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');
}
}
}
?>
$ mkdir src/Controller $ touch src/Controller/BadzillaStaticSearchController.php
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 $request, SearchPageInterface $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;
}
}
?>


$ drush cr [success] Cache rebuild complete.