Skip to content

Commit

Permalink
Fix search (#55)
Browse files Browse the repository at this point in the history
- Add missing search page cacheable metadata.
- Add template for tags search results.
  • Loading branch information
Niklan authored Jan 13, 2025
1 parent 0017d12 commit d28ff2a
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
<h2 class="search-result__title">
<a href="{{ url }}" class="search-result__link">{{ title }}</a>
</h2>
<div class="search-result__excerpt">
{{ excerpt }}
</div>
{% if excerpt %}
<div class="search-result__excerpt">
{{ excerpt }}
</div>
{% endif %}
</article>
6 changes: 6 additions & 0 deletions app/Drupal/laszlo/src/Hook/Theme/PreprocessTaxonomyTerm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{% include 'laszlo:search-result' with {
title: term.label(),
url: url_absolute,
} only %}
4 changes: 4 additions & 0 deletions app/Drupal/niklan/src/Search/Controller/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
Expand Down

0 comments on commit d28ff2a

Please sign in to comment.