search.pages.inc 2.2 KB

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