Issue #2567091 by slashrsm: AJAX updates of an element in a #group are not working
parent
8dc402f97d
commit
1909ebd759
|
@ -81,6 +81,15 @@ class FormAjaxResponseBuilder implements FormAjaxResponseBuilderInterface {
|
||||||
$response = $result;
|
$response = $result;
|
||||||
}
|
}
|
||||||
else {
|
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 */
|
/** @var \Drupal\Core\Ajax\AjaxResponse $response */
|
||||||
$response = $this->ajaxRenderer->renderResponse($result, $request, $this->routeMatch);
|
$response = $this->ajaxRenderer->renderResponse($result, $request, $this->routeMatch);
|
||||||
}
|
}
|
||||||
|
|
|
@ -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');
|
||||||
|
}
|
||||||
|
}
|
|
@ -36,4 +36,12 @@ class Callbacks {
|
||||||
$response->addCommand(new DataCommand('#ajax_checkbox_value', 'form_state_value_select', (int) $form_state->getValue('checkbox')));
|
$response->addCommand(new DataCommand('#ajax_checkbox_value', 'form_state_value_select', (int) $form_state->getValue('checkbox')));
|
||||||
return $response;
|
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 @@ 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;
|
return $form;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue