RenderedItem::addFieldValues() may not switch back to the correct session

Created on 29 September 2023, over 1 year ago
Updated 7 October 2023, over 1 year ago

Problem/Motivation

Drupal\search_api\Plugin\search_api\processor\RenderedItem::addFieldValues() switches to another session in a foreach loop over the fields using AccountSwitcher::switchTo(), but the AccountSwitcher::switchBack() method is only called once after the foreach loop. This may result in the wrong session being active at the end of the processing depending on index configuration.

Discussion

Since switchBack() will stack original accounts and use array_pop() from that stack (i.e. LIFO or last in first out) when switching back, you need to call it once per switchTo() to switch back all the way to the first account on the stack.

The current implementation in search_api may result in a destroyed session when there are multiple fields that contain rendered items, because then switchBack() is not called often enough. Also, if for some reason the processor is called in that stage, but there are no fields returned, you might get a "No more accounts to revert to." exception, because switchTo() was never called when you try to switch back, although this latter thing is just a theoretical issue as the processor should not be added in such cases in the first place.

Steps to reproduce

Create an index with more than one "rendered_item" field.

I noticed this on the devel solr debug page provided by search_api_solr_devel sub-module of search_api_solr. For a node, that was indexed in an index that used two rendered items rendered as anonymous user the admin toolbar would not be visible on that page and the devel dumper output in the field sets showing the values sent to solr and the values indexed by solr were missing as well. It did work for indexes that had only one rendered item field or none at all.

Proposed resolution

Switch back at the end of the foreach loop instead of after the foreach loop.

Remaining tasks

🐛 Bug report
Status

Fixed

Version

1.0

Component

Plugins

Created by

🇩🇪Germany FeyP

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

Comments & Activities

Production build 0.71.5 2024