From d28ff2a9020cdd9ad7ade11af51278704542e007 Mon Sep 17 00:00:00 2001 From: Nikita Malyshev Date: Mon, 13 Jan 2025 19:46:19 +0500 Subject: [PATCH] Fix search (#55) - Add missing search page cacheable metadata. - Add template for tags search results. --- .../content/search-result/search-result.component.yml | 2 +- .../components/content/search-result/search-result.twig | 8 +++++--- .../laszlo/src/Hook/Theme/PreprocessTaxonomyTerm.php | 6 ++++++ .../tags/taxonomy-term--tags--search-result.html.twig | 4 ++++ app/Drupal/niklan/src/Search/Controller/Search.php | 4 ++++ 5 files changed, 20 insertions(+), 4 deletions(-) create mode 100644 app/Drupal/laszlo/templates/content/taxonomy-term/tags/taxonomy-term--tags--search-result.html.twig diff --git a/app/Drupal/laszlo/components/content/search-result/search-result.component.yml b/app/Drupal/laszlo/components/content/search-result/search-result.component.yml index 016d0844..a39bdc3b 100644 --- a/app/Drupal/laszlo/components/content/search-result/search-result.component.yml +++ b/app/Drupal/laszlo/components/content/search-result/search-result.component.yml @@ -12,5 +12,5 @@ props: type: string description: The URL of the search result. excerpt: - type: [string, array] + type: [string, array, 'null'] description: The excerpt of the search result. diff --git a/app/Drupal/laszlo/components/content/search-result/search-result.twig b/app/Drupal/laszlo/components/content/search-result/search-result.twig index fab681ce..85c19d50 100644 --- a/app/Drupal/laszlo/components/content/search-result/search-result.twig +++ b/app/Drupal/laszlo/components/content/search-result/search-result.twig @@ -2,7 +2,9 @@

{{ title }}

-
- {{ excerpt }} -
+ {% if excerpt %} +
+ {{ excerpt }} +
+ {% endif %} \ No newline at end of file diff --git a/app/Drupal/laszlo/src/Hook/Theme/PreprocessTaxonomyTerm.php b/app/Drupal/laszlo/src/Hook/Theme/PreprocessTaxonomyTerm.php index 294dc047..69b18b2a 100644 --- a/app/Drupal/laszlo/src/Hook/Theme/PreprocessTaxonomyTerm.php +++ b/app/Drupal/laszlo/src/Hook/Theme/PreprocessTaxonomyTerm.php @@ -22,10 +22,16 @@ public static function create(ContainerInterface $container): self { ); } + private function addCommonVariables(TermInterface $term, array &$variables): void { + $variables['url_absolute'] = $term->toUrl()->setAbsolute()->toString(); + } + public function __invoke(array &$variables): void { $term = $variables['term']; \assert($term instanceof TermInterface); + $this->addCommonVariables($term, $variables); + $class = match ($term->bundle()) { default => NULL, 'tags' => PreprocessTaxonomyTermTags::class, diff --git a/app/Drupal/laszlo/templates/content/taxonomy-term/tags/taxonomy-term--tags--search-result.html.twig b/app/Drupal/laszlo/templates/content/taxonomy-term/tags/taxonomy-term--tags--search-result.html.twig new file mode 100644 index 00000000..368a73f5 --- /dev/null +++ b/app/Drupal/laszlo/templates/content/taxonomy-term/tags/taxonomy-term--tags--search-result.html.twig @@ -0,0 +1,4 @@ +{% include 'laszlo:search-result' with { + title: term.label(), + url: url_absolute, +} only %} \ No newline at end of file diff --git a/app/Drupal/niklan/src/Search/Controller/Search.php b/app/Drupal/niklan/src/Search/Controller/Search.php index fb474381..3fcca096 100644 --- a/app/Drupal/niklan/src/Search/Controller/Search.php +++ b/app/Drupal/niklan/src/Search/Controller/Search.php @@ -82,6 +82,10 @@ public function __invoke(Request $request): array { '#query' => $query, '#pager' => ['#type' => 'pager', '#quantity' => 5], '#cache' => [ + 'contexts' => [ + 'url.query_args:q', + 'url.query_args.pagers:0', + ], 'tags' => [ 'search_api_list:global_index', ],