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