Potentially Invalid array_search() call in Multiselect.php

Created on 19 April 2023, over 1 year ago
Updated 26 March 2024, 8 months ago

Problem/Motivation

We ran into this after upgrading from 1.2 to 2.0, using PHP 8,0 on Drupal 9.5

Editing an existing node that contains a multiselect throws the error:

The website encountered an unexpected error. Please try again later.
TypeError: array_search(): Argument #2 ($haystack) must be of type array, string given in array_search() (line 92 of modules/contrib/multiselect/src/Element/Multiselect.php).
array_search('6', '6') (Line: 92)
Drupal\multiselect\Element\Multiselect::getOptions('selected', Array) (Line: 76)
template_preprocess_multiselect(Array, 'multiselect', Array)
call_user_func_array('template_preprocess_multiselect', Array) (Line: 287)

Steps to reproduce

  1. Create a node that has multiselect data -- maybe using v 1.2? -- and only assign 1 value to the item
  2. Upgrade to 2.0
  3. Try to save the node without making any changes
  4. Fatal error thrown

Proposed resolution

The problem seems to be that the code expects $options[array_search($key, $element['#value'])] to have $element['#value'] be an array.

In our case, only one item is selected so it seems to be a string.

Ref: https://www.php.net/manual/en/function.array-search.php

Remaining tasks

I am not sure why we need array_search() here. Maybe just return $key?
Otherwise, we have to check that values are an array first.

User interface changes

None

API changes

None

Data model changes

None

πŸ› Bug report
Status

Needs review

Version

2.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States agentrickard Georgia (US)

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

Merge Requests

Comments & Activities

Production build 0.71.5 2024