search.pages.inc 2.3 KB

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