How do I change this markup from my .theme file?

Line 119 and following in core/modules/search/src/Controller/SearchController.php contain this code.

if (count($results)) {
  $build['search_results_title'] = array(
    '#markup' => '<h2>' . $this->t('Search results') . '</h2>',

I want to remove the <h2> tags from my search page.

I am able to change the search form above, and the result list after that tag using hook_preprocess_form()on the search form and hook_preprocess_search_result() on the search results.

Is there a preprocess function I am missing, or can I use a custom template?


This answer has already been answered on stackoverflow. Here's a copy paste of what Cornel Andreev wrote.

You have to alter the route defined by the search module. In order to do so:

  1. Define in your mymodule.services.yml file following:

      class: Drupal\mymodule\Routing\RouteSubscriber
        - { name: event_subscriber }

  1. Create a class that extends the RouteSubscriberBase class on /mymodule/src/Routing/RouteSubscriber.php as following:

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

    namespace Drupal\mymodule\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 our own.
        if ($route = $collection->get('search.view_node_search')) {
          $route->setDefault('_controller', '\Drupal\mymodule\Controller\MyModuleSearchController::view');

  1. Finally, the controller itself located on /mymodule/src/Controller/MyModuleSearchController.php

    namespace Drupal\mymodule\Controller;

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

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

       * {@inheritdoc}
      public function view(Request $request, SearchPageInterface $entity) {
        $build = parent::view($request, $entity);
        // Unset the Result title.
        if (isset($build['search_results_title'])) {

        return $build;



I had a similar problem wherein i had to hide the Search Results H2 tag entirely.

I could manage to do it with Jquery with the below code :

$('h2:contains("Search results")').css("display", "none");

protected by kiamlaluno Feb 12 at 5:39

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.