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

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

ryrye created an issue. See original summary.

katrialesser’s picture

I'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

  • 's in search results are always empty.

    Any idea how we can consistently provide an overriding template?

  • Mario Steinitz’s picture

    Assigned: Unassigned » Mario Steinitz
    FSar’s picture

    I 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.

    Nicolas Bouteille’s picture

    This is probably a bug in core as I am facing the same problem without this module enabled.

    vinyl_roads’s picture

    I 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.

    grgcrlsn321’s picture

    I 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.

    Tarun bansal’s picture

    Can anyone tell me how can i show images in search result page in drupal 8 from twig file?

    Thanks in advance.

    Vishakha Sharma’s picture

    I 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';
    }
    }

    andrenoronha’s picture

    I'm facing the same problem. #9 did not work for me.

    Rishi Kulshreshtha’s picture

    @andrenoronha, try the below code, it should work as it worked for me:

    /**
     * Implements hook_theme_registry_alter().
     */
    function mytheme_theme_registry_alter(&$theme_registry) {
      $theme_handler = \Drupal::service('theme_handler');
      $default_theme = $theme_handler->getDefault();
      $theme_path = $theme_handler->getTheme($default_theme)->getPath();
      $theme_registry['search_result']['path'] = $theme_path . '/templates/search';
    }
    

    NB: Don't forget to clear the cache. Cheers!

    demonde’s picture

    The code from #11 gives an error message if the template is not present.

    The website encountered an unexpected error. Please try again later.
    Twig\Error\LoaderError: Template "themes/enstrap_sub/templates/search/custom-search-result.html.twig" is not defined. 
    demonde’s picture

    Priority: Normal » Major
    Mario Steinitz’s picture

    Assigned: Mario Steinitz » Unassigned
    Status: Active » Needs review
    FileSize
    17.02 KB

    Attached 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/

    Mario Steinitz’s picture

    FileSize
    17.59 KB

    Some additional tweaks to really override the item list.

    aiphes’s picture

    Hello,
    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:

    modules/contrib/custom_search $ curl https://www.drupal.org/files/issues/2020-10-09/custom_sea rch-2885485-15.patch | git apply
      % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                     Dload  Upload   Total   Spent    Left  Speed
    100 18013  100 18013    0     0  54750      0 --:--:-- --:--:-- --:--:-- 54750

    then:

    <!-- FILE NAME SUGGESTIONS:
       * item-list--search-results.html.twig
       x custom-search-results.html.twig
       * item-list.html.twig
    -->

    so I can style the template as desired like in D8.

    aiphes’s picture

    With Custom Search (custom_search) Enabled 8.x-1.0-beta9 , suggestion disappear.
    Do I need to repatch ?
    EDIt: no.

    modules/contrib/custom_search$ curl https://www.drupal.org/files/issues/2020-10-09/custom_search-2885485-15.patch | git apply
      % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                     Dload  Upload   Total   Spent    Left  Speed
    100 18013  100 18013    0     0  56823      0 --:--:-- --:--:-- --:--:-- 56823
    error: patch failed: custom_search.module:455
    error: custom_search.module: patch does not apply
    Guido_S’s picture

    Version: 8.x-1.0-beta6 » 8.x-1.0-beta9
    Status: Needs review » Needs work
    Issue tags: +Needs reroll

    Patch #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.

    Guido_S’s picture

    Status: Needs work » Needs review
    Issue tags: -Needs reroll
    FileSize
    1.72 KB
    17.64 KB

    I 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

    aiphes’s picture

    Thanks to #19. It works for the beta9.
    Search Custom Search (custom_search) Enabled 8.x-1.0-beta9

    modules/contrib/custom_search$ curl https://www.drupal.org/files/issues/2023-04-24/custom_search-2885485-19.patch | git apply
      % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                     Dload  Upload   Total   Spent    Left  Speed
    100 18063  100 18063    0     0  42601      0 --:--:-- --:--:-- --:--:-- 42601
    
    cedric_a’s picture

    Status: Needs review » Needs work

    I 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 :

    <!-- THEME DEBUG -->
    <!-- THEME HOOK: 'search_result' -->
    <!-- FILE NAME SUGGESTIONS:
       * search-result--node-search.html.twig
       * search-result.html.twig
    -->
    <!-- INVALID FILE NAME SUGGESTIONS:
       See https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Render!theme.api.php/function/hook_theme_suggestions_alter
       custom_search_result__node_search
       custom_search_result
    -->
    <!-- BEGIN OUTPUT from 'themes/thematik/templates/custom-search-result.html.twig' -->
    [...]
    <!-- END OUTPUT from 'themes/thematik/templates/custom-search-result.html.twig' -->
    

    So I looked at the api doc mentionned and spoted this :

    New suggestions must begin with the value of HOOK, followed by two underscores to be discoverable.

    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.