Unverified Commit 4d6e1e22 authored by Alex Pott's avatar Alex Pott
Browse files

fix: #3569751 Workspace AJAX query parameters become arrays when form elements...

fix: #3569751 Workspace AJAX query parameters become arrays when form elements have pre-existing token/persist values

By: tim bozeman
By: amateescu
(cherry picked from commit 86416c4c)
parent d5aa01a4
Loading
Loading
Loading
Loading
Loading
+4 −5
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@
use Drupal\Core\Form\WorkspaceDynamicSafeFormInterface;
use Drupal\Core\Form\WorkspaceSafeFormInterface;
use Drupal\Core\Hook\Attribute\Hook;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Render\Element;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\workspaces\Entity\Workspace;
@@ -129,11 +130,9 @@ protected function setAjaxWorkspace(array &$element, array $url_query_options):
      }
    }

    if (isset($element['#ajax']) && !isset($element['#ajax']['options']['query']['workspace'])) {
      $element['#ajax']['options']['query'] = array_merge_recursive(
        $url_query_options,
        $element['#ajax']['options']['query'] ?? [],
      );
    if (isset($element['#ajax'])) {
      $existing_query = $element['#ajax']['options']['query'] ?? [];
      $element['#ajax']['options']['query'] = NestedArray::mergeDeep($existing_query, $url_query_options);
    }
  }

+11 −0
Original line number Diff line number Diff line
@@ -61,6 +61,17 @@ public function buildForm(array $form, FormStateInterface $form_state): array {
        },
      ],
    ];
    $form['collision_test'] = [
      '#type' => 'textfield',
      '#ajax' => [
        'url' => Url::fromRoute('workspaces_test.get_form'),
        'options' => [
          'query' => [
            'media_library_opener_id' => 'test',
          ],
        ],
      ],
    ];
    return $form;
  }

+7 −0
Original line number Diff line number Diff line
@@ -76,6 +76,13 @@ public function testFormPersistence(): void {
    $form_state_1 = new FormState();
    $form_1 = $this->formBuilder->buildForm($form_arg, $form_state_1);

    $this->assertSame([
      'media_library_opener_id' => 'test',
      'workspace' => 'ham',
      'token' => $form_1['collision_test']['#ajax']['options']['query']['token'],
      'persist' => FALSE,
    ], $form_1['collision_test']['#ajax']['options']['query']);

    $this->switchToWorkspace('cheese');
    $form_state_2 = new FormState();
    $this->formBuilder->buildForm($form_arg, $form_state_2);