Same value is shown for two different view fields when using the entity field rendering

Created on 23 April 2019, over 5 years ago
Updated 22 August 2024, 3 months ago

Problem/Motivation

I have made a custom views field formatter, but when I used it on two different fields, I got same results for those two fields. I'm using search API, and both field are based on two media entities.

The bug also happens with standard views field formatters.

How to reproduce

  1. Enable the media module.
  2. Create a media bundle: picto, using the image media type (this will add the field_media_image automatically).
  3. Create a media content for the image and picto bundles.
  4. On a node bundle, add two media fields, one for the image bundle (field_img), the other for the picto bundle (field_picto).
  5. Create a node using the two media content.
  6. Create new search API index with node in index, and field_img>entity>field_media_image + field_picto>entity>field_media_image as fields
  7. Create a new view on search api index, add both indexed fields in Use entity field rendering mode.
  8. In the preview, both fields show the same value (image).

=> if not formatter is set (use entity id to rendering) we can see it's a result problem, search API got the correct entity referenced; it's when views use same formatter on same field

Additional notes

In a custom formatter code, it's always the first field image using the Use entity field rendering mode which is sent in the viewElements() methods.

🐛 Bug report
Status

Active

Version

11.0 🔥

Component
Views 

Last updated about 12 hours ago

Created by

🇫🇷France musa.thomas France 🇫🇷

Live updates comments and jobs are added and updated live.
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.

  • 🇳🇮Nicaragua edysmp Nicaragua

    I found something similar with the paragraphs and search_api contrib modules.

    scenario:
    - display paragraphs' fields in a search api view.
    - multiple paragraphs are attached to a node

    search api view result:
    title, paragraph-title, paragraph-text
    my article, "paragraph 1, paragraph 2, paragraph 3, paragraph 4", "my text 1, my text 4, my text 4, my text 4"

    in the example the bolded text result are wrong. it should be "my text 2" and "my text 3", but the last paragraph content is displayed instead "my text 4". this is the same behavior if view's field are interchanged:

    my article,"my text 1, my text 2, my text 3, my text 4" "paragraph 1, paragraph 4, paragraph 4, paragraph 4"

    debugged the issue and the related code is in `Drupal\views\Entity\Render\EntityFieldRenderer::buildFields`.

    i came with a fix that is to pass the rendering `$field` to the buildFields method so the valueIndex from the search_api code refers to the correct field. in the current code the $field is always gathered from the first field witch when rendering the second field gets the wrong valueIndex value which is used to get the actual paragraph entity.

    the issue could be fixed in search_api itself because it extends the `EntityFieldRenderer` class, but maybe it worth finding a solution in core.

  • 🇳🇮Nicaragua edysmp Nicaragua

    attaching the patch. take a look and advice on next steps. ty.

  • 🇮🇳India vinmayiswamy

    Hi, I followed the steps outlined in the issue summary but couldn't reproduce the issue. The view correctly displays the proper images for both fields.

    My local setup specifications:

    1. Drupal 11.x
    2. PHP 8.3
    3. Search API 8.x-1.35
    4. Default database server for indexing
    5. Forma

    tter: Render Entity

    Everything worked as expected, with no issues in image rendering. If there are any additional steps or scenarios that I may have overlooked, or if there are other aspects I should consider, I would greatly appreciate any guidance.

    Thanks!

  • 🇮🇳India vinmayiswamy

    Attaching screenshots for reference.

Production build 0.71.5 2024