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

8.1.x
Nathaniel Catchpole 2016-02-12 10:33:12 +09:00
parent 8dc402f97d
commit 1909ebd759
4 changed files with 97 additions and 0 deletions

View File

@ -81,6 +81,15 @@ class FormAjaxResponseBuilder implements FormAjaxResponseBuilderInterface {
$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);
}

View File

@ -0,0 +1,38 @@
<?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');
}
}

View File

@ -36,4 +36,12 @@ class Callbacks {
$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'];
}
}

View File

@ -72,6 +72,48 @@ class AjaxFormsTestSimpleForm extends FormBase {
);
}
$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;
}