Skip to content

element_children() error in flattenKeys() on scalar keyword inputΒ #19

@bobchristenson

Description

@bobchristenson

πŸ› Bug: element_children() error in flattenKeys() on scalar keyword input

Project: search_api_solr
Backdrop CMS version: 1.x (tested on 1.27.2)
Module version: 1.x (latest dev)
Search backend: OpenSolr (hosted Solr service)


Summary

When performing certain keyword searches, this module throws a fatal error:

User error: "0" is an invalid render array key in element_children() 
in element_children() (line 7548 of core/includes/common.inc)

The error occurs in the method SearchApiSolrService::flattenKeys() in includes/service.inc. It incorrectly assumes that $keys is a render array and passes it to element_children(), even though $keys may contain scalars with numeric keys.


Steps to Reproduce

  1. Configure a Solr server using OpenSolr.
  2. Create a Search API index and attach it to a View.
  3. Add a fulltext keyword filter to the View.
  4. Perform a keyword search using a simple word (e.g., christmas).

In some cases, the $keys array passed into flattenKeys() looks like this:

[
  0 => 'christmas',
  '#conjunction' => 'OR',
]

This causes element_children() to throw an error, as 0 => 'christmas' is not a valid child in a render array.


Why This Happens

The function element_children() is only intended for render arrays. Here, $keys is just a structured array of search conditions and not meant for rendering. Calling element_children() on such data will break when numeric or scalar values are included.


Proposed Fix

Replace this line:

foreach (element_children($keys) as $i) {

With this:

foreach ($keys as $i => $key) {
  if (strpos($i, '#') === 0) {
    continue; // Skip metadata keys like #conjunction
  }

This change avoids element_children() entirely, filters metadata manually, and is safe for scalar/non-render values.


Patch

A working patch is attached below:
βœ… Prevents the error
βœ… Keeps all original behavior
βœ… Safe for scalar + nested array input

```diff diff --git a/includes/service.inc b/includes/service.inc index 1234567..abcdef0 100644 --- a/includes/service.inc +++ b/includes/service.inc @@ protected function flattenKeys(array $keys) { - foreach (element_children($keys) as $i) { + foreach ($keys as $i => $key) { + if (strpos($i, '#') === 0) { + continue; // Skip metadata keys like #conjunction or #negation + } ```
---

Recommendation

  • βœ… Consider removing all usage of element_children() on non-render data for future robustness.
  • πŸ” This issue is exposed by OpenSolr, but the underlying logic bug could affect any site given the right input structure.

πŸ“Ž Patch File (Download)

Patch file is attached

search_api_solr-flattenKeys-fix.patch

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions