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

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

8
use Drupal\Component\Utility\String;
9
use Drupal\Core\Language\LanguageInterface;
10 11 12 13 14 15

/**
 * Implements hook_theme_suggestions_HOOK().
 */
function search_theme_suggestions_search_result(array $variables) {
  return array('search_result__' . $variables['plugin_id']);
16 17 18
}

/**
19
 * Prepares variables for individual search result templates.
20
 *
21 22 23
 * Default template: search-result.html.twig
 *
 * @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 28 29 30 31 32
 *   - 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.
33
 */
34
function template_preprocess_search_result(&$variables) {
35
  $language_interface = \Drupal::languageManager()->getCurrentLanguage();
36

37 38
  $result = $variables['result'];
  $variables['url'] = check_url($result['link']);
39
  $variables['title'] = String::checkPlain($result['title']);
40
  if (isset($result['language']) && $result['language'] != $language_interface->getId() && $result['language'] != LanguageInterface::LANGCODE_NOT_SPECIFIED) {
41 42
    $variables['title_attributes']['lang'] = $result['language'];
    $variables['content_attributes']['lang'] = $result['language'];
43
  }
44

45
  $info = array();
46
  if (!empty($result['plugin_id'])) {
47
    $info['plugin_id'] = String::checkPlain($result['plugin_id']);
48
  }
49 50
  if (!empty($result['user'])) {
    $info['user'] = $result['user'];
51
  }
52
  if (!empty($result['date'])) {
53
    $info['date'] = format_date($result['date'], 'short');
54
  }
55 56
  if (isset($result['extra']) && is_array($result['extra'])) {
    $info = array_merge($info, $result['extra']);
57
  }
58 59 60 61 62
  // 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;
  $variables['info'] = implode(' - ', $info);
63 64
}