Commit 1909ebd7 authored by catch's avatar catch

Issue #2567091 by slashrsm: AJAX updates of an element in a #group are not working

parent 8dc402f9
......@@ -81,6 +81,15 @@ public function buildResponse(Request $request, array $form, FormStateInterface
$response = $result;
}
else {
// At this point we know callback returned a render element. If the
// element is part of the group (#group is set on it) it won't be rendered
// unless we remove #group from it. This is caused by
// \Drupal\Core\Render\Element\RenderElement::preRenderGroup(), which
// prevents all members of groups from being rendered directly.
if (!empty($result['#group'])) {
unset($result['#group']);
}
/** @var \Drupal\Core\Ajax\AjaxResponse $response */
$response = $this->ajaxRenderer->renderResponse($result, $request, $this->routeMatch);
}
......
<?php
/**
* @file
* Contains \Drupal\system\Tests\Ajax\AjaxInGroupTest.
*/
namespace Drupal\system\Tests\Ajax;
use Drupal\Core\Ajax\DataCommand;
/**
* Tests that form elements in groups work correctly with AJAX.
*
* @group Ajax
*/
class AjaxInGroupTest extends AjaxTestBase {
protected function setUp() {
parent::setUp();
$this->drupalLogin($this->drupalCreateUser(array('access content')));
}
/**
* Submits forms with select and checkbox elements via Ajax.
*/
function testSimpleAjaxFormValue() {
$this->drupalGet('/ajax_forms_test_get_form');
$this->assertText('Test group');
$this->assertText('AJAX checkbox in a group');
$this->drupalPostAjaxForm(NULL, ['checkbox_in_group' => TRUE], 'checkbox_in_group');
$this->assertText('Test group');
$this->assertText('AJAX checkbox in a group');
$this->assertText('AJAX checkbox in a nested group');
$this->assertText('Another AJAX checkbox in a nested group');
}
}
......@@ -36,4 +36,12 @@ function checkboxCallback($form, FormStateInterface $form_state) {
$response->addCommand(new DataCommand('#ajax_checkbox_value', 'form_state_value_select', (int) $form_state->getValue('checkbox')));
return $response;
}
/**
* Ajax callback triggered by the checkbox in a #group.
*/
function checkboxGroupCallback($form, FormStateInterface $form_state) {
return $form['checkbox_in_group_wrapper'];
}
}
......@@ -72,6 +72,48 @@ public function buildForm(array $form, FormStateInterface $form_state) {
);
}
$form['test_group'] = [
'#type' => 'details',
'#title' => $this->t('Test group'),
'#open' => TRUE,
];
// Test ajax element in a #group.
$form['checkbox_in_group_wrapper'] = [
'#type' => 'container',
'#attributes' => ['id' => 'checkbox-wrapper'],
'#group' => 'test_group',
'checkbox_in_group' => [
'#type' => 'checkbox',
'#title' => $this->t('AJAX checkbox in a group'),
'#ajax' => [
'callback' => [$object, 'checkboxGroupCallback'],
'wrapper' => 'checkbox-wrapper',
],
],
'nested_group' => [
'#type' => 'details',
'#title' => $this->t('Nested group'),
'#open' => TRUE,
],
'checkbox_in_nested' => [
'#type' => 'checkbox',
'#group' => 'nested_group',
'#title' => $this->t('AJAX checkbox in a nested group'),
'#ajax' => [
'callback' => [$object, 'checkboxGroupCallback'],
'wrapper' => 'checkbox-wrapper',
],
],
];
$form['another_checkbox_in_nested'] = [
'#type' => 'checkbox',
'#group' => 'nested_group',
'#title' => $this->t('Another AJAX checkbox in a nested group'),
];
return $form;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment