Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Very difficult/impossible to override the template for individual search results: custom-search-result.html-twig
I'm able to override it by placing search-result--node-search.html.twig in my theme's template dir, but it only works the first time after a full cache flush. After page reload, the theme engine reverts to using the template file provided in the custom_search module: custom-search-result.html.twig
Comment | File | Size | Author |
---|---|---|---|
#19 | custom_search-2885485-19.patch | 17.64 KB | Guido_S |
Comments
Comment #2
katrialesser CreditAttribution: katrialesser commentedI'm also having issues with this. In my case, I'm trying to override it to fix some issue with Bootstrap (the search result is never printed out, so the
Any idea how we can consistently provide an overriding template?
Comment #3
Mario SteinitzComment #4
FSar CreditAttribution: FSar commentedI am having the same issue. Only bootstrap is not showwing the results. I have been trying to print the array and getting the array without the 'value' index which is used to display the result.
Comment #5
Nicolas Bouteille CreditAttribution: Nicolas Bouteille commentedThis is probably a bug in core as I am facing the same problem without this module enabled.
Comment #6
vinyl_roadsI am having the same issue. Did you find a solution ?
If I use in my custom theme item-list--custom-search-results--.html.twig, it works randomly (maybe 1st time after cache flush like ryrye said.
But after, I have :
<!-- BEGIN OUTPUT from 'modules/contrib/custom_search/templates/custom-search-results.html.twig' -->
And I can't override item-list--custom-search-results--.html.twig and the the result is not correct (I can see the list of results but without title.
Comment #7
grgcrlsn321 CreditAttribution: grgcrlsn321 as a volunteer commentedI was having trouble with this too and not able to override the custom search templates.
Ended up ditching the module and was able to override the drupal core search result template with
item-list--search-results.twig.html
or any of the other suggested templates. So, I do not believe this is a bug with drupal core mentioned in Comment #5 above.Comment #8
Tarun bansal CreditAttribution: Tarun bansal commentedCan anyone tell me how can i show images in search result page in drupal 8 from twig file?
Thanks in advance.
Comment #9
Vishakha Sharma CreditAttribution: Vishakha Sharma commentedI tried to override the custom search template by specifying theme path in hook_theme_registry_alter and it worked for me.
I Uploaded custom-search-result.html.twig and custom-search-results.html.twig file inside root directory of templates folder of my theme.
function yourtheme_theme_registry_alter(&$theme_registry) {
if (isset($theme_registry['item_list__search_results'])) {
$theme_registry['item_list__search_results']['path'] = drupal_get_path('theme', 'yourtheme') . '/templates';
}
if (isset($theme_registry['search_result'])) {
$theme_registry['search_result']['path'] = drupal_get_path('theme', 'yourtheme') . '/templates';
}
}
Comment #10
andrenoronha CreditAttribution: andrenoronha as a volunteer commentedI'm facing the same problem. #9 did not work for me.
Comment #11
Rishi Kulshreshtha@andrenoronha, try the below code, it should work as it worked for me:
NB: Don't forget to clear the cache. Cheers!
Comment #12
demonde CreditAttribution: demonde commentedThe code from #11 gives an error message if the template is not present.
Comment #13
demonde CreditAttribution: demonde commentedComment #14
Mario SteinitzAttached a patch that addresses the issue by preventing the custom_search module from altering the template theme registration. Instead, it just merges additional variables and adds the custom search module templates as template suggestions to Core item_list__search_results and search_result.
This approach worked fine for theme template overrides. Possible side-effects with other search modules need to be explored though.
Please test the patch/
Comment #15
Mario SteinitzSome additional tweaks to really override the item list.
Comment #16
aiphesHello,
After upgrade from D8, my custom-search twig template doesn't work anymore with #9, so I try with #11.
But I get this message:
Warning: Undefined array key "plugin_id" in custom_search_theme_suggestions_item_list__search_results() (line 108 of modules/contrib/custom_search/custom_search.pages.inc).
does the patch could be help ?
thanks
EDIT: applied patch seem to work:
then:
so I can style the template as desired like in D8.
Comment #17
aiphesWith
Custom Search (custom_search) Enabled 8.x-1.0-beta9
, suggestion disappear.Do I need to repatch ?
EDIt: no.
Comment #18
Guido_SPatch #15 doesn't apply to beta-9 and search-result.html.twig in my theme won't get used although it's the suggested file name.
So the patch needs at least a reroll.
Comment #19
Guido_SI rerolled the patch from #15 and resolved merge conflicts in the rebase process which occurred in the custom_search_theme_registry_alter() function. Those conflicts were a result from replaced deprecated functions.
So I removed the lines just like patch #15 did.
The rerolled patch should apply to 8.x-1.0-beta9
Comment #20
aiphesThanks to #19. It works for the beta9.
Search Custom Search (custom_search) Enabled 8.x-1.0-beta9
Comment #21
cedric_aI just tested the patch #19 on a fresh Drupal 10.1.5 with Custom Search 8.x-1.0-beta9 and I could override the template custom-search-result.html.twig by copying it from the module folder to my custom theme folder.
However, the twig debug is showing this error message in the markup :
So I looked at the api doc mentionned and spoted this :
I tested to change the suggestions from "custom_search_result__" to "search_result__", it removes the warning and show the template used with an "x".
But I think that it's not the suggestion name that we want because it is the name of the core search result template.
Should we adopt a suggestion name like "search_result__custom_search__" ?
I'm wondering if these changes will break backward compatibility too.