From 911ee4bfe0d85f70d69d775b3420637558d4ebb6 Mon Sep 17 00:00:00 2001 From: Paul Pound Date: Thu, 15 Oct 2026 10:28:35 -0300 Subject: [PATCH] made module advanced_search aware --- README.md | 4 ++- mirador_search_highlight.module | 50 +++++++++++++++++++++++++++------ 2 files changed, 45 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 23015b6..9460a40 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,9 @@ so matches are highlighted with no extra JavaScript. ## How it works -1. `hook_views_pre_render()` appends `?search_api_fulltext=` to the +1. `hook_views_pre_render()` reads the search term (from Advanced Search's + `a[N][v]` parameters, or a plain Search API fulltext filter) and appends + `?search_api_fulltext=` to the "Link this field to its item" links in the `solr_search_content` view. 2. `hook_preprocess_mirador()` reads that parameter and sets `windows[0].defaultSearchQuery` on the viewer's Mirador config. diff --git a/mirador_search_highlight.module b/mirador_search_highlight.module index 929887a..af8cdbf 100644 --- a/mirador_search_highlight.module +++ b/mirador_search_highlight.module @@ -5,6 +5,8 @@ * Runs a IIIF content search in Mirador on load from a passed search term. */ +use Drupal\advanced_search\AdvancedSearchQuery; +use Drupal\advanced_search\AdvancedSearchQueryTerm; use Drupal\views\ViewExecutable; /** @@ -29,13 +31,8 @@ function mirador_search_highlight_views_pre_render(ViewExecutable $view) { if (!in_array($view->id(), MIRADOR_SEARCH_HIGHLIGHT_VIEWS, TRUE)) { return; } - if (!method_exists($view->query, 'getOriginalKeys')) { - return; - } - // Parse modes may turn the keys into an array; only the raw string is - // useful as a Mirador search query. - $keys = $view->query->getOriginalKeys(); - if (!is_string($keys) || trim($keys) === '') { + $keys = _mirador_search_highlight_get_keys($view); + if ($keys === '') { return; } @@ -45,9 +42,46 @@ function mirador_search_highlight_views_pre_render(ViewExecutable $view) { // links, and an explicit 'query' replaces the item URL's query. // UrlHelper::buildQuery() encodes the value before Views token // replacement, so user input is never treated as Twig. - $handler->options['alter']['query'] = [MIRADOR_SEARCH_HIGHLIGHT_PARAM => trim($keys)]; + $handler->options['alter']['query'] = [MIRADOR_SEARCH_HIGHLIGHT_PARAM => $keys]; + } + } +} + +/** + * Gets the search term the view was run with. + * + * @param \Drupal\views\ViewExecutable $view + * The search view. + * + * @return string + * The search term, or an empty string if there is none. + */ +function _mirador_search_highlight_get_keys(ViewExecutable $view) { + // Advanced Search passes terms as a[N][v] and writes them straight into the + // Solr query, so the Search API query never has keys set. Read the terms + // the same way it does, skipping excluded ("IS NOT") terms. + if (class_exists(AdvancedSearchQuery::class) && ($request = $view->getRequest())) { + $values = []; + foreach ((new AdvancedSearchQuery())->getTerms($request) as $term) { + if ($term->getInclude()) { + $values[] = trim((string) ($term->toQueryParams()[AdvancedSearchQueryTerm::VALUE_QUERY_PARAMETER] ?? '')); + } + } + $keys = implode(' ', array_filter($values, 'strlen')); + if ($keys !== '') { + return $keys; + } + } + + // Fall back to a plain Search API fulltext filter. Parse modes may turn the + // keys into an array; only the raw string is useful to Mirador. + if (method_exists($view->query, 'getOriginalKeys')) { + $keys = $view->query->getOriginalKeys(); + if (is_string($keys)) { + return trim($keys); } } + return ''; } /**