Update search-result.html.twig code comments to reflect correct $info_split default keys

Created on 22 September 2017, almost 7 years ago
Updated 18 April 2023, over 1 year ago

Problem/Motivation

In search-result.html.twig I see the following code comments:

 * Available variables:
 * - url: URL of the result.
 * ...
 * - info_split: Contains same data as info, but split into separate parts.
 *   - info_split.type: Node type (or item type string supplied by module).

In fact, the 'type' key has not been available since Drupal 6. The corresponding code comments in search-result.tpl.php (Drupal 7) were updated in #1517032: Update search-result.tpl.php code comments to reflect new $info_split default keys β†’ .

Proposed resolution

Update the code comments, deleting the reference to info_split.type and adding a reference to info_split.plugin_id. This key is provided by template_preprocess_search_result() and corresponds to the 'module' key in Drupal 7.

Also document that the raw results provided by the search plugin are available to the template as {{ result }}.

Work-arounds

For node searches, the type is already available in the template as {{ result.type }}.

If you want to make {{ info_split.type }} available to the template, then you can add the following to mytheme.theme:

/**
 * Implements hook_preprocess_HOOK() for search_result.
 *
 * Make the (node) type available to search-result.html.twig.
 */
function mytheme_preprocess_search_result(&$variables) {
  $info = &$variables['info_split'];
  $result = $variables['result'];
  if (empty($info['type']) && !empty($result['type'])) {
    $info['type'] = $result['type'];
  }
}

Then you can access the type inside search-results.html.twig as {{ info_split.type }}.

You might also want to add, inside the if block,

  $variables['info']['#context']['info'] = $info;

Then when you use {{ info }} inside the Twig template, it should be updated to include the type.
See template_preprocess_search_result(). I have not tested this.

Remaining tasks

  1. (Comments #3, #8)
  2. (Not needed: Comment #7.)

User interface changes

None

API changes

None

Data model changes

None

πŸ› Bug report
Status

Needs work

Version

9.5

Component
SearchΒ  β†’

Last updated 5 days ago

  • Maintained by
  • πŸ‡ΊπŸ‡ΈUnited States @pwolanin
Created by

πŸ‡ΊπŸ‡ΈUnited States benjifisher Boston area

Live updates comments and jobs are added and updated live.
  • Novice

    It would make a good project for someone who is new to the Drupal contribution process. It's preferred over Newbie.

  • Needs issue summary update

    Issue summaries save everyone time if they are kept up-to-date. See Update issue summary task instructions.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Thank you @rudrakumar188 and @Arti Anil Pattewar but screenshots of the patch applying doesn't provide any value as we know that from the CI build. See https://www.drupal.org/about/core/policies/maintainers/how-is-credit-gra... β†’ for more info.

    THe changes in #10 look fine but appears some things have changed from the proposed solution in the issue summary.

    If that's the case can that be updated please.

  • πŸ‡ͺπŸ‡ΈSpain alejandrosrez Barcelona

    Is this fixed?
    I ran into the same obsolete comments in a 9.5.2 installation. I needed to implement a custom hook in the .theme file to be able to use the info_split variable as is explained in the comments of search-result.html.twig. I see that this issue is open but unassigned and as I'm new to Drupal contributions I'm not sure about the next steps to take.

  • πŸ‡ͺπŸ‡ΈSpain alejandrosrez Barcelona

    Is this issue fixed?

    I ran into the same obsolete commits in the file search-result.html.twig in a 9.5.2 installation. I needed to implement a custom hook in the .theme file to use info_split as is explained in search-result.html.twig's comments. I see that the issue is open and unasssigned. What are the next steps to fix it? I am new to contributing to Drupal and are unsure about what the status of the issue is. Thank you.

Production build 0.71.5 2024