Problem/Motivation

When FormOperations::setAjaxWorkspace() processes a form element that already has token or persist in its #ajax['options']['query'] (but no workspace key), the use of array_merge_recursive() converts these scalar values into arrays. This causes Symfony's InputBag::get() to throw a BadRequestException: "Input value contains a non-scalar value."

Steps to reproduce

This can occur when modules like Media Library copy request parameters to form elements, and those parameters already contain workspace-related values from a previous AJAX request. Hopefully (or hopefully not?), others run into this and help with the steps to reproduce because I'm not totally sure how to reproduce this on vanilla Drupal core because my use case has a lot of stuff going on, but it is something like

  • Enable Workspaces module
  • Create and switch to a non-default workspace
  • Build a workspace-safe form with an AJAX element that has pre-existing token or persist query params (simulating what happens when Media Library copies request params)
  • The resulting #ajax['options']['query'] will contain arrays instead of scalars:
    • token becomes ['workspace-token', 'pre-existing-token']
    • persist becomes [false, '']

Proposed resolution

Replace array_merge_recursive() with array_replace() in FormOperations::setAjaxWorkspace(). This ensures workspace parameters always remain scalar values, with the current workspace's values taking precedence over any pre-existing values.

<?php
  // Before
  if (isset($element['#ajax']) && !isset($element['#ajax']['options']['query']['workspace'])) {
    $element['#ajax']['options']['query'] = array_merge_recursive(
      $url_query_options,
      $element['#ajax']['options']['query'] ?? [],
    );
  }

  // After
  if (isset($element['#ajax'])) {
    $existing_query = $element['#ajax']['options']['query'] ?? [];
    $element['#ajax']['options']['query'] = NestedArray::mergeDeep($existing_query, $url_query_options);
  }
?>

Issue fork drupal-3569751

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

tim bozeman created an issue. See original summary.

quietone’s picture

Version: 11.3.x-dev » main
Issue summary: View changes
Status: Needs work » Active

The main development branch is 'main'. Changes are made on the main branch first, and are then back ported as needed according to the Core change policies. The version the problem was discovered on should be stated in the issue summary Problem/Motivation section. Thanks.

There is no merge request here so changing status.

shubham_pareek_19 made their first commit to this issue’s fork.

shubham_pareek_19 changed the visibility of the branch 3569751-workspace-ajax-query to hidden.

tim bozeman changed the visibility of the branch 3569751-workspace-ajax-query to active.

tim bozeman’s picture

Issue summary: View changes
tim bozeman’s picture

Status: Active » Needs review
tim bozeman’s picture

Issue summary: View changes
amateescu’s picture

Status: Needs review » Needs work

Reviewed the MR :)

tim bozeman’s picture

Status: Needs work » Needs review

Thank you for reviewing amateescu! I consolidated the test form and assertions.

amateescu’s picture

Status: Needs review » Reviewed & tested by the community

Looks great now :)

alexpott’s picture

Version: main » 11.3.x-dev
Status: Reviewed & tested by the community » Fixed

Committed and pushed 86416c4ca4e to main and 58d66871534 to 11.x and 4d6e1e226cd to 11.3.x. Thanks!

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

  • alexpott committed 4d6e1e22 on 11.3.x
    fix: #3569751 Workspace AJAX query parameters become arrays when form...

  • alexpott committed 58d66871 on 11.x
    fix: #3569751 Workspace AJAX query parameters become arrays when form...

  • alexpott committed 86416c4c on main
    fix: #3569751 Workspace AJAX query parameters become arrays when form...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.