4

I want to remove the Search results string added in the search page from line 119 of core/modules/search/src/Controller\SearchController.php.

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

I am able to change the search form above and the result list using preprocess_form function on the search form and preprocess_search_result on the search results.

Is there a preprocess function I missed, or can I use a custom template file?

3 Answers 3

5

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:

    services:
      mymodule.route_subscriber:
      class: Drupal\mymodule\Routing\RouteSubscriber
      tags:
        - { name: event_subscriber }

  1. Create a class that extends the RouteSubscriberBase class on /mymodule/src/Routing/RouteSubscriber.php as following:
<?php
    /**
     * @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
<?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'])) {
          unset($build['search_results_title']);
        }
    
        return $build;
      }
    
    }

0

Solution from @hugronaphor works perfect. I wanted my search results title to be "Search results for '(searchterm)'" instead of just "Search results" and steps described by @hugronaphor do exactly that.

In my view function I put this:

if (isset($build['search_results_title']) && isset($_GET['keys'])) {
   $build['search_results_title'] = ['#markup' => '<h2>' . t('Search results for') . ' "' . $_GET['keys'] . '"</h2>'];
}
-1

You can override item-list--search-results.html.twig, and replace the title, here:

  {%- if title is not empty -%}
    <h3>{{ title }}</h3>
  {%- endif -%}

Just remove that h3.

1
  • You are speaking about another template. The thread starter actually wants to know how to manipulate the search page controller's output. Because the render array with the key 'search_results_title' is simple markup and has no theme oder type key, one needs to manipulate the controller output directly.
    – alexej_d
    Mar 17, 2017 at 14:57

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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