search.pages.inc 2.17 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * User page callbacks for the Search module.
6 7
 */

8
use Drupal\Component\Utility\UrlHelper;
9
use Drupal\Core\Language\LanguageInterface;
10

11
/**
12
 * Prepares variables for individual search result templates.
13
 *
14 15 16
 * Default template: search-result.html.twig
 *
 * @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 21 22 23 24 25
 *   - title_prefix: Additional output populated by modules, intended to be
 *     displayed in front of the main title tag that appears in the template.
 *   - title_suffix: Additional output populated by modules, intended to be
 *     displayed after the main title tag that appears in the template.
 *   - title_attributes: HTML attributes for the title.
 *   - content_attributes: HTML attributes for the content.
26
 */
27
function template_preprocess_search_result(&$variables) {
28
  $language_interface = \Drupal::languageManager()->getCurrentLanguage();
29

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 35
    $variables['title_attributes']['lang'] = $result['language'];
    $variables['content_attributes']['lang'] = $result['language'];
36
  }
37

38
  $info = [];
39
  if (!empty($result['plugin_id'])) {
40
    $info['plugin_id'] = $result['plugin_id'];
41
  }
42 43
  if (!empty($result['user'])) {
    $info['user'] = $result['user'];
44
  }
45
  if (!empty($result['date'])) {
46
    $info['date'] = format_date($result['date'], 'short');
47
  }
48 49
  if (isset($result['extra']) && is_array($result['extra'])) {
    $info = array_merge($info, $result['extra']);
50
  }
51 52 53 54
  // Check for existence. User search does not include snippets.
  $variables['snippet'] = isset($result['snippet']) ? $result['snippet'] : '';
  // Provide separated and grouped meta information..
  $variables['info_split'] = $info;
55
  $variables['info'] = [
56 57
    '#type' => 'inline_template',
    '#template' => '{{ info|safe_join(" - ") }}',
58 59
    '#context' => ['info' => $info],
  ];
60
}