5 * User page callbacks for the Search module.
8 use Drupal\Component\Utility\UrlHelper;
9 use Drupal\Core\Language\LanguageInterface;
12 * Prepares variables for individual search result templates.
14 * Default template: search-result.html.twig
16 * @param array $variables
17 * An array with the following elements:
18 * - result: Individual search result.
19 * - plugin_id: Plugin the search results came from.
20 * - title_prefix: Additional output populated by modules, intended to be
21 * displayed in front of the main title tag that appears in the template.
22 * - title_suffix: Additional output populated by modules, intended to be
23 * displayed after the main title tag that appears in the template.
24 * - title_attributes: HTML attributes for the title.
25 * - content_attributes: HTML attributes for the content.
27 function template_preprocess_search_result(&$variables) {
28 $language_interface = \Drupal::languageManager()->getCurrentLanguage();
30 $result = $variables['result'];
31 $variables['url'] = UrlHelper::stripDangerousProtocols($result['link']);
32 $variables['title'] = $result['title'];
33 if (isset($result['language']) && $result['language'] != $language_interface->getId() && $result['language'] != LanguageInterface::LANGCODE_NOT_SPECIFIED) {
34 $variables['title_attributes']['lang'] = $result['language'];
35 $variables['content_attributes']['lang'] = $result['language'];
39 if (!empty($result['plugin_id'])) {
40 $info['plugin_id'] = $result['plugin_id'];
42 if (!empty($result['user'])) {
43 $info['user'] = $result['user'];
45 if (!empty($result['date'])) {
46 $info['date'] = format_date($result['date'], 'short');
48 if (isset($result['extra']) && is_array($result['extra'])) {
49 $info = array_merge($info, $result['extra']);
51 // Check for existence. User search does not include snippets.
52 $variables['snippet'] = isset($result['snippet']) ? $result['snippet'] : '';
53 // Provide separated and grouped meta information..
54 $variables['info_split'] = $info;
55 $variables['info'] = [
56 '#type' => 'inline_template',
57 '#template' => '{{ info|safe_join(" - ") }}',
58 '#context' => ['info' => $info],