TypeError: Illegal offset type in isset or empty in Drupal\search_api\Entity\Index->isValidDatasource() when using rendered item field

Created on 4 October 2022, over 1 year ago
Updated 21 February 2023, over 1 year ago

Problem/Motivation

If I index an item and then try to render it with the "Rendered item" field in Views, Search API crashes:

TypeError: Illegal offset type in isset or empty in Drupal\search_api\Entity\Index->isValidDatasource() (line 358 of /var/www/html/web/modules/contrib/search_api/src/Entity/Index.php).

Drupal\search_api\Entity\Index->isValidDatasource(Array) (Line: 137)
Drupal\search_api\Plugin\views\field\SearchApiRenderedItem->render(Object) (Line: 1158)
Drupal\views\Plugin\views\field\FieldPluginBase->advancedRender(Object) (Line: 238)

This is ultimately because search_api_datasource is an array, and the code only ever expects it to be a string.

I initially thought this was a side effect of πŸ› All fields being indexed as arrays Active but Elasticsearch Connector doesn't suffer from the same issue. It turns out that in fact Elasticsearch Connector does not index the search_api_id, search_api_datasource or search_api_language fields separately. Instead, the Search API ResultRow object has a magic getter and special code to lazy-load these values from methods on the original Item object:

  /**
   * The lazy-loaded properties, as property names mapped to item methods.
   *
   * @var string[]
   */
  protected static $lazyLoad = [
    'search_api_id' => 'getId',
    'search_api_datasource' => 'getDatasourceId',
    'search_api_language' => 'getLanguage',
    'search_api_relevance' => 'getScore',
    'search_api_excerpt' => 'getExcerpt',
  ];

Commenting out the call to $this->addSpecialFields() in IndexParamBuilder::buildIndexParams() avoids these fields ever being added to the index in the first place.

Steps to reproduce

Create a view that uses the "Rendered item" field, and use it to render a search result.

Proposed resolution

Remove IndexParamBuilder::addSpecialFields().

Remaining tasks

User interface changes

API changes

Data model changes

πŸ› Bug report
Status

Postponed: needs info

Version

2.0

Component

Code

Created by

πŸ‡¬πŸ‡§United Kingdom longwave UK

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.

Production build 0.69.0 2024