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