search-result.html.twig

Default theme implementation for displaying a single search result.

This template renders a single search result and is collected into search-results.html.twig. This and the parent template are dependent to one another sharing the markup for ordered lists.

Available variables:

  • url: URL of the result.
  • title: Title of the result.
  • snippet: A small preview of the result. Does not apply to user searches.
  • info: String of all the meta information ready for print. Does not apply to user searches.
  • module: The machine-readable name of the module (tab) being searched, such as "node" or "user".
  • 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.
  • info_split: Contains same data as info, but split into separate parts.
    • info_split.type: Node type (or item type string supplied by module).
    • info_split.user: Author of the node linked to users profile. Depends on permission.
    • info_split.date: Last update of the node. Short formatted.
    • info_split.comment: Number of comments output as "% comments", % being the count. (Depends on comment.module).

@todo The info variable needs to be made drillable and each of these sub items should instead be within info and renamed info.foo, info.bar, etc.

Other variables:

  • title_attributes: HTML attributes for the title.
  • content_attributes: HTML attributes for the content.

Since info_split is keyed, a direct print of the item is possible. This array does not apply to user searches so it is recommended to check for its existence before printing. The default keys of 'type', 'user' and 'date' always exist for node searches. Modules may provide other data.


  {% if (info_split.comment) %}
    <span class="info-comment">
      {{ info_split.comment }}
    </span>
  {% endif %}

To check for all available data within info_split, use the code below.


  <pre>
    {{ dump(info_split) }}
  </pre>

See also

template_preprocess()

template_preprocess_search_result()

template_process()

3 theme calls to search-result.html.twig
hook_search_page in drupal/core/modules/search/search.api.php
Override the rendering of search results.
search_extra_type_search_page in drupal/core/modules/search/tests/modules/search_extra_type/search_extra_type.module
Implements hook_search_page().
template_preprocess_search_results in drupal/core/modules/search/search.pages.inc
Prepares variables for search results templates.

File

drupal/core/modules/search/templates/search-result.html.twig
View source
  1. {#
  2. /**
  3. * @file
  4. * Default theme implementation for displaying a single search result.
  5. *
  6. * This template renders a single search result and is collected into
  7. * search-results.html.twig. This and the parent template are
  8. * dependent to one another sharing the markup for ordered lists.
  9. *
  10. * Available variables:
  11. * - url: URL of the result.
  12. * - title: Title of the result.
  13. * - snippet: A small preview of the result. Does not apply to user searches.
  14. * - info: String of all the meta information ready for print. Does not apply
  15. * to user searches.
  16. * - module: The machine-readable name of the module (tab) being searched, such
  17. * as "node" or "user".
  18. * - title_prefix: Additional output populated by modules, intended to be
  19. * displayed in front of the main title tag that appears in the template.
  20. * - title_suffix: Additional output populated by modules, intended to be
  21. * displayed after the main title tag that appears in the template.
  22. * - info_split: Contains same data as info, but split into separate parts.
  23. * - info_split.type: Node type (or item type string supplied by module).
  24. * - info_split.user: Author of the node linked to users profile. Depends
  25. * on permission.
  26. * - info_split.date: Last update of the node. Short formatted.
  27. * - info_split.comment: Number of comments output as "% comments", %
  28. * being the count. (Depends on comment.module).
  29. * @todo The info variable needs to be made drillable and each of these sub
  30. * items should instead be within info and renamed info.foo, info.bar, etc.
  31. *
  32. * Other variables:
  33. * - title_attributes: HTML attributes for the title.
  34. * - content_attributes: HTML attributes for the content.
  35. *
  36. * Since info_split is keyed, a direct print of the item is possible.
  37. * This array does not apply to user searches so it is recommended to check
  38. * for its existence before printing. The default keys of 'type', 'user' and
  39. * 'date' always exist for node searches. Modules may provide other data.
  40. * @code
  41. * {% if (info_split.comment) %}
  42. * <span class="info-comment">
  43. * {{ info_split.comment }}
  44. * </span>
  45. * {% endif %}
  46. * @endcode
  47. *
  48. * To check for all available data within info_split, use the code below.
  49. * @code
  50. * <pre>
  51. * {{ dump(info_split) }}
  52. * </pre>
  53. * @endcode
  54. *
  55. * @see template_preprocess()
  56. * @see template_preprocess_search_result()
  57. * @see template_process()
  58. *
  59. * @ingroup themeable
  60. */
  61. #}
  62. <li {{ attributes }}>
  63. {{ title_prefix }}
  64. <h3 class="title"{{ title_attributes }}>
  65. <a href="{{ url }}">{{ title }}</a>
  66. </h3>
  67. {{ title_suffix }}
  68. <div class="search-snippet-info">
  69. {% if snippet %}
  70. <p class="search-snippet"{{ content_attributes }}>{{ snippet }}</p>
  71. {% endif %}
  72. {% if info %}
  73. <p class="search-info">{{ info }}</p>
  74. {% endif %}
  75. </div>
  76. </li>

Related topics