Subrata Sarkar
Subrata Sarkar

Reputation: 3064

Drupal 8 - how to customise search result template

I have a custom content type with several fields like Price, Product Summary, Product image etc. Is there any way I can access these fields separately on search result page?

I printed the rendered array on mytheme/item-list.html.twig using kint(item.value) which shows that I can only access title, url and type separately like {{ item.value['#result'].url }}, {{ item.value['#result'].title }}. But don't see variables like product_image, product_summary etc. in rendered array.

According to Twig template suggestion the template I need to override is search-result.html.twig. So I copied /core/themes/stable/templates/content/search-result.html.twig to my theme's folder. However, the {{ snippet }} variable does not have information I want to access.

I went through the following steps to customise the result output:

  1. Went to /admin/structure/types/manage/product/display (Structure > Content Types > Product > Manage Display). Turned on Search Index and Search result highlight input.

enter image description here

  1. Navigated to Search result highlight input tab and added (enabled) two fields Product summary and Product price

enter image description here

  1. Went back to search result but I still cannot see the those variables in rendered array.

Can somebody please tell me the best way to accomplish the above?

Upvotes: 2

Views: 6570

Answers (1)

Harshita Saini
Harshita Saini

Reputation: 64

@Subrata Sarkar There is one option. Firstly,install two module search_api and search_api_page module from drupal.org

https://www.drupal.org/project/search_api

https://www.drupal.org/project/search_api_page enter image description here

Add server and index in search api. Add the fields which you want to display on page within index and after all configuration indexed all your data. enter image description here

After this create and configure search page.And choose view mode option and select a view mode ex: Search result highlighting input or Search index etc. enter image description here

Hope you get your desired result on search page.For more information, https://www.youtube.com/watch?v=dNilEVQu94I

Upvotes: 1

Related Questions