show date in search results (drupal 10)

41 Views Asked by At

I have a site in Drupal version 10.2.2 When I search in Search

title
content 

Lists the results in the form of a list. I want to show the date information next to the title information. Where and how can I do this?

I couldn't find the module that solves this job. I guess it will be solved with twig?

1

There are 1 best solutions below

1
Kien Nguyen On

Here's a way you can achieve this:

  1. Create a custom theme (if not already done)

  2. Override the search result template by creating search-result--node-search.html.twig file in your custom theme's templates directory

{{ title_prefix }}
<h3{{ title_attributes }}>
  <a href="{{ url }}">{{ title }}</a>
</h3>
{{ title_suffix }}
{% if snippet %}
  <p{{ content_attributes }}>{{ snippet }}</p>
{% endif %}
{% if info %}
  <p>{{ info }}</p>
{% endif %}
<span class="search-result__date">{{ content['#node'].field_date.value|date('Y-m-d') }}</span>
  1. Clear the cache