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) === '') { return; } foreach ($view->field as $handler) { if (!empty($handler->options['link_to_item'])) { // Search API items are merged with $options['alter'] when rendered as // 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)]; } } } /** * Implements hook_preprocess_HOOK() for mirador. * * Runs after template_preprocess_mirador(), so mirador_view_id is already the * unique ID used as the drupalSettings viewer key. */ function mirador_search_highlight_preprocess_mirador(&$variables) { // Vary the cached viewer by the search term, otherwise the first term // searched would be cached and shown to everyone viewing this item. $variables['#cache']['contexts'][] = 'url.query_args:' . MIRADOR_SEARCH_HIGHLIGHT_PARAM; $term = \Drupal::request()->query->get(MIRADOR_SEARCH_HIGHLIGHT_PARAM, ''); $term = is_string($term) ? trim($term) : ''; if ($term === '') { return; } $selector = '#' . $variables['mirador_view_id']; $variables['#attached']['drupalSettings']['mirador']['viewers'][$selector]['windows'][0]['defaultSearchQuery'] = mb_substr($term, 0, 256); }