Unverified Commit e30e925a authored by alexpott's avatar alexpott
Browse files

Issue #2983551 by eiriksm, tim.plunkett: When using ajax inside an add block...

Issue #2983551 by eiriksm, tim.plunkett: When using ajax inside an add block form, multiple components are added to the section
parent e9dd745e
......@@ -48,10 +48,10 @@ protected function submitLabel() {
*/
public function buildForm(array $form, FormStateInterface $form_state, SectionStorageInterface $section_storage = NULL, $delta = NULL, $region = NULL, $plugin_id = NULL) {
// Only generate a new component once per form submission.
if (!$component = $form_state->getTemporaryValue('layout_builder__component')) {
if (!$component = $form_state->get('layout_builder__component')) {
$component = new SectionComponent($this->uuidGenerator->generate(), $region, ['id' => $plugin_id]);
$section_storage->getSection($delta)->appendComponent($component);
$form_state->setTemporaryValue('layout_builder__component', $component);
$form_state->set('layout_builder__component', $component);
}
return $this->doBuildForm($form, $form_state, $section_storage, $delta, $component);
}
......
<?php
namespace Drupal\layout_builder_test\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Provides a 'TestAjax' block.
*
* @Block(
* id = "layout_builder_test_testajax",
* admin_label = @Translation("TestAjax"),
* category = @Translation("Test")
* )
*/
class TestAjaxBlock extends BlockBase {
/**
* {@inheritdoc}
*/
public function blockForm($form, FormStateInterface $form_state) {
$form['ajax_test'] = [
'#type' => 'radios',
'#options' => [
1 => $this->t('Ajax test option 1'),
2 => $this->t('Ajax test option 2'),
],
'#prefix' => '<div id="test-ajax-wrapper">',
'#suffix' => '</div>',
'#title' => $this->t('Time in this ajax test is @time', [
'@time' => time(),
]),
'#ajax' => [
'wrapper' => 'test-ajax-wrapper',
'callback' => [$this, 'ajaxCallback'],
],
];
return $form;
}
/**
* Ajax callback.
*/
public function ajaxCallback($form, $form_state) {
return $form['settings']['ajax_test'];
}
/**
* {@inheritdoc}
*/
public function build() {
$build['content'] = [
'#markup' => $this->t('Every word is like an unnecessary stain on silence and nothingness.'),
];
return $build;
}
}
<?php
namespace Drupal\Tests\layout_builder\FunctionalJavascript;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
/**
* Ajax blocks tests.
*
* @group layout_builder
*/
class AjaxBlockTest extends WebDriverTestBase {
/**
* {@inheritdoc}
*/
public static $modules = [
'block',
'node',
'datetime',
'layout_builder',
'user',
'layout_builder_test',
];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$user = $this->drupalCreateUser([
'configure any layout',
'administer node display',
'administer node fields',
]);
$user->save();
$this->drupalLogin($user);
$this->createContentType(['type' => 'bundle_with_section_field']);
}
/**
* Tests configuring a field block for a user field.
*/
public function testAddAjaxBlock() {
$assert_session = $this->assertSession();
$page = $this->getSession()->getPage();
// Start by creating a node.
$node = $this->createNode([
'type' => 'bundle_with_section_field',
'body' => [
[
'value' => 'The node body',
],
],
]);
$node->save();
$this->drupalGet('node/1');
$assert_session->pageTextContains('The node body');
$assert_session->pageTextNotContains('Every word is like an unnecessary stain on silence and nothingness.');
$field_ui_prefix = 'admin/structure/types/manage/bundle_with_section_field';
// From the manage display page, go to manage the layout.
$this->drupalGet("$field_ui_prefix/display/default");
$assert_session->linkExists('Manage layout');
$this->clickLink('Manage layout');
$assert_session->addressEquals("$field_ui_prefix/display-layout/default");
// The body field is present.
$assert_session->elementExists('css', '.field--name-body');
// Add a new block.
$assert_session->linkExists('Add Block');
$this->clickLink('Add Block');
$assert_session->assertWaitOnAjaxRequest();
$assert_session->linkExists('TestAjax');
$this->clickLink('TestAjax');
$assert_session->assertWaitOnAjaxRequest();
// Find the radio buttons.
$name = 'settings[ajax_test]';
/** @var \Behat\Mink\Element\NodeElement[] $radios */
$radios = $this->cssSelect('input[name="' . $name . '"]');
// Click them both a couple of times.
foreach ([1, 2] as $rounds) {
foreach ($radios as $radio) {
$radio->click();
$assert_session->assertWaitOnAjaxRequest();
}
}
// Then add the block.
$page->pressButton('Add Block');
$assert_session->assertWaitOnAjaxRequest();
$block_elements = $this->cssSelect('.block-layout-builder-test-testajax');
// Should be exactly one of these in there.
$this->assertEquals(1, count($block_elements));
$assert_session->pageTextContains('Every word is like an unnecessary stain on silence and nothingness.');
}
}
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