diff --git a/core/lib/Drupal/Core/Render/Element/RenderElement.php b/core/lib/Drupal/Core/Render/Element/RenderElement.php index f7ad74b..ca4806a 100644 --- a/core/lib/Drupal/Core/Render/Element/RenderElement.php +++ b/core/lib/Drupal/Core/Render/Element/RenderElement.php @@ -240,7 +240,12 @@ public static function preRenderAjaxForm($element) { // to hide the password field dynamically. $query = \Drupal::request()->query->all(); $query[FormBuilderInterface::AJAX_FORM_REQUEST] = TRUE; - $settings['url'] = Url::fromRoute('', [], ['query' => $query])->toString(); + + $settings += ['options' => []]; + $settings['options'] += ['query' => []]; + $options['query'] = $settings['options']['query'] + $query; + + $settings['url'] = Url::fromRoute('', [], $options)->toString(); } // @todo Legacy support. Remove in Drupal 8. diff --git a/core/modules/system/src/Tests/Ajax/AjaxFormCacheTest.php b/core/modules/system/src/Tests/Ajax/AjaxFormCacheTest.php index f151c33..cc91f03 100644 --- a/core/modules/system/src/Tests/Ajax/AjaxFormCacheTest.php +++ b/core/modules/system/src/Tests/Ajax/AjaxFormCacheTest.php @@ -44,8 +44,9 @@ public function testFormCacheUsage() { $this->drupalGet($cached_form_url); $this->drupalGet($cached_form_url); - // The number of cache entries should be exactly 3. - $this->assertEqual(3, count($key_value_expirable->getAll())); + // The number of cache entries should be exactly 0, we just start to cache + // once we have a POST request. + $this->assertEqual(0, count($key_value_expirable->getAll())); } /** diff --git a/core/modules/system/tests/modules/ajax_forms_test/src/Form/AjaxFormsTestCachedForm.php b/core/modules/system/tests/modules/ajax_forms_test/src/Form/AjaxFormsTestCachedForm.php index 3b22783..61122bf 100644 --- a/core/modules/system/tests/modules/ajax_forms_test/src/Form/AjaxFormsTestCachedForm.php +++ b/core/modules/system/tests/modules/ajax_forms_test/src/Form/AjaxFormsTestCachedForm.php @@ -34,9 +34,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { 'option1' => $this->t('Option 1'), 'option2' => $this->t('Option 2'), ], - '#ajax' => [ - 'url' => Url::fromRoute('system.ajax'), - ], + '#ajax' => [], ]; return $form; } diff --git a/core/tests/Drupal/Tests/Core/Render/Element/RenderElementTest.php b/core/tests/Drupal/Tests/Core/Render/Element/RenderElementTest.php index 2f1bf66..bdbf5ef 100644 --- a/core/tests/Drupal/Tests/Core/Render/Element/RenderElementTest.php +++ b/core/tests/Drupal/Tests/Core/Render/Element/RenderElementTest.php @@ -21,17 +21,38 @@ class RenderElementTest extends UnitTestCase { /** + * The request stack. + * + * @var \Symfony\Component\HttpFoundation\RequestStack + */ + protected $requestStack; + + /** + * The container. + * + * @var \Drupal\Core\DependencyInjection\ContainerBuilder + */ + protected $container; + + /** + * {@inheritdoc} + */ + protected function setUp() { + parent::setUp(); + + $this->requestStack = new RequestStack(); + $this->container = new ContainerBuilder(); + $this->container->set('request_stack', $this->requestStack); + \Drupal::setContainer($this->container); + } + + /** * @covers ::preRenderAjaxForm */ public function testPreRenderAjaxForm() { - $request_stack = new RequestStack(); - $container = new ContainerBuilder(); - $container->set('request_stack', $request_stack); - \Drupal::setContainer($container); - $request = Request::create('/test'); $request->query->set('foo', 'bar'); - $request_stack->push($request); + $this->requestStack->push($request); $prophecy = $this->prophesize('Drupal\Core\Routing\UrlGeneratorInterface'); $url = '/test?foo=bar&ajax_form=1'; @@ -39,7 +60,7 @@ public function testPreRenderAjaxForm() { ->willReturn($url); $url_generator = $prophecy->reveal(); - $container->set('url_generator', $url_generator); + $this->container->set('url_generator', $url_generator); $element = [ '#type' => 'select', @@ -55,4 +76,41 @@ public function testPreRenderAjaxForm() { $this->assertTrue($element['#ajax_processed']); $this->assertEquals($url, $element['#attached']['drupalSettings']['ajax']['test']['url']); } + + /** + * @covers ::preRenderAjaxForm + */ + public function testPreRenderAjaxFormWithQueryOptions() { + $request = Request::create('/test'); + $request->query->set('foo', 'bar'); + $this->requestStack->push($request); + + $prophecy = $this->prophesize('Drupal\Core\Routing\UrlGeneratorInterface'); + $url = '/test?foo=bar&other=query&ajax_form=1'; + $prophecy->generateFromRoute('', [], ['query' => ['foo' => 'bar', 'other' => 'query', FormBuilderInterface::AJAX_FORM_REQUEST => TRUE]], FALSE) + ->willReturn($url); + + $url_generator = $prophecy->reveal(); + $this->container->set('url_generator', $url_generator); + + $element = [ + '#type' => 'select', + '#id' => 'test', + '#ajax' => [ + 'wrapper' => 'foo', + 'callback' => 'test-callback', + 'options' => [ + 'query' => [ + 'other' => 'query', + ] + ] + ], + ]; + + $element = RenderElement::preRenderAjaxForm($element); + + $this->assertTrue($element['#ajax_processed']); + $this->assertEquals($url, $element['#attached']['drupalSettings']['ajax']['test']['url']); + } + }