EntityField::getValue() can return inconsistent results

Created on 20 December 2023, 6 months ago
Updated 26 December 2023, 6 months ago

Problem/Motivation

SearchApiEntityField extends EntityField. It does not override EntityField::getValue(). EntityField::getValue() calls $this->getEntity() which is overridden so SearchApiEntityField::getEntity(). This relies on the value of $this->valueIndex (from SearchApiFieldTrait). If not previously used the value 0 will be used. If however the field has been used previously by SearchApiEntityField::getItems() it will keep whatever the last value was set to.

Steps to reproduce

Ha! Iterate over views results retrieving the values with getItems for a field. Make sure the last one has multiple values. Then iterate of the results again for the field, but just try and get the value with getValue(). So the code I'm staring at is assuming a single value, or at least will only use the first value. At least that's what I think it's supposed to do.

Proposed resolution

Well this is more documentation for now! It shouldn't produce inconsistent results, ie. what value in valueIndex from another method previously for another row shouldn't make a difference to this method when called. But then again I'm not sure what getValue() should return in this context really.

πŸ› Bug report
Status

Active

Version

1.0

Component

Views integration

Created by

πŸ‡³πŸ‡±Netherlands ekes

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @ekes
  • πŸ‡¦πŸ‡ΉAustria drunken monkey Vienna, Austria

    Interesting problem, thanks for reporting.
    I’m far from sure myself, but it seems like we might just want to reset $this->valueIndex to 0 after the foreach loop in SearchApiEntityField::getItems()?

Production build 0.69.0 2024