PHP Implode error array vs string, EntityReferenceSelection

Created on 26 October 2023, about 1 year ago

Problem/Motivation

I've generated a simple example to demonstrate:
Say we don't want users able to delete nodes they have created, but instead we have a webform where they can request a specific node they created to be removed. We would get notified and remove it for them if we approve. The webform contains an element that uses a view to show a list of nodes the current user is the last revision author for, so they can select from that list and submit. That element of course references a specific view and passes a token for the current user to the view for the argument.

Steps to reproduce

Create an entity reference view that requires an argument (example bug_view yaml file included)
Create a webform that Entity Checkboxes populated by that view, passing an argument. (example bug_webform yaml file included)
Edit the Entity Checkboxes element, choose the Conditions tab, and attempt to add a row to the state or another state.

Nothing happens.

If you view the network activity, you can see behind the scenes an error was thrown (and in the Drupal logs):

TypeError: implode(): Argument #2 ($array) must be of type ?array, string given in implode() (line 164 of /web/core/modules/views/src/Plugin/EntityReferenceSelection/ViewsSelection.php)

Proposed resolution

I'm not sure where the error originates. The value that should be an array is the string argument given in the View Arguments setting of the webform element. Suggest altering line 164 from:
$default = !empty($view_settings['arguments']) ? implode(', ', $view_settings['arguments']) : '';
to:
$default = !empty($view_settings['arguments']) && is_array($view_settings['arguments'] ? implode(', ', $view_settings['arguments']) : '';

In my case I can work around this time by using the view default argument to pull the logged in userid and set the webform element argument as empty. The solution needs to accommodate multiple view arguments, which my recommendation does if the user comma separates them.

Remaining tasks

User interface changes

API changes

Data model changes

🐛 Bug report
Status

Active

Version

6.1

Component

Code

Created by

🇺🇸United States srees

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

Comments & Activities

  • Issue created by @srees
  • Assigned to Shreya_98
  • First commit to issue fork.
  • 🇺🇸United States jrockowitz Brooklyn, NY
  • Status changed to Closed: won't fix 9 months ago
  • 🇺🇸United States jrockowitz Brooklyn, NY

    This feels like an edge case. The view could easily be written to default to the current user id with requiring it to be passed via the webform.

  • Issue was unassigned.
  • Status changed to Needs work about 2 months ago
  • 🇺🇸United States djdevin Philadelphia

    This is still an issue, the problem is that the form rebuilds when you add another condition, and then the arguments end up being a string instead of an array.

    This only happens if you try to add more than 1 condition to an Entity reference views component.

Production build 0.71.5 2024