Commit f453c2d4 authored by acbramley's avatar acbramley Committed by fenstrat

Issue #2887516 by acbramley, fenstrat: Errors set on validateConfigurationForm...

Issue #2887516 by acbramley, fenstrat: Errors set on validateConfigurationForm halt form submission but are not displayed
parent e7f28734
......@@ -244,7 +244,20 @@ class BlockFieldWidget extends WidgetBase implements ContainerFactoryPluginInter
// Execute block validate configuration.
$block_instance = $block_manager->createInstance($plugin_id, $settings);
$settings = (new FormState())->setValues($settings);
$block_instance->validateConfigurationForm($form, $settings);
$block_instance->validateConfigurationForm($element['settings'], $settings);
// Pass along errors from the block validation.
foreach ($settings->getErrors() as $key => $error) {
$parents = implode('][', $element['settings']['#parents']);
// If the block form used setError() then the parents will already be
// part of the key since we are passing along the element in the context
// of the whole form. If the block form used setErrorByName we need to
// add the parents in.
if (strpos($key, $parents) === FALSE) {
$key = sprintf('%s][%s', $parents, $key);
}
$form_state->setErrorByName($key, $error);
}
NestedArray::setValue($values, $element['settings']['#parents'], $settings->getValues());
$form_state->setValues($values);
......
<?php
namespace Drupal\block_field_test\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Provides a 'Block field test validation' block.
*
* @Block(
* id = "block_field_test_validation",
* admin_label = @Translation("Block field test validation"),
* category = @Translation("Block field test")
* )
*/
class BlockFieldTestValidationBlock extends BlockBase {
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'content' => '',
];
}
/**
* {@inheritdoc}
*/
public function blockForm($form, FormStateInterface $form_state) {
$form['content'] = [
'#type' => 'textfield',
'#title' => $this->t('Content'),
'#default_value' => $this->configuration['content'],
];
return $form;
}
/**
* {@inheritdoc}
*/
public function blockSubmit($form, FormStateInterface $form_state) {
$this->configuration['content'] = $form_state->getValue('content');
}
/**
* {@inheritdoc}
*/
public function blockValidate($form, FormStateInterface $form_state) {
if ($form_state->getValue('content') == 'error by name') {
$form_state->setErrorByName('content', 'Come ere boi!');
}
if ($form_state->getValue('content') == 'error by element') {
$form_state->setError($form['content'], 'Gimmie them toez!');
}
}
/**
* {@inheritdoc}
*/
public function build() {
return [
'#type' => 'markup',
'#markup' => $this->configuration['content'],
];
}
}
......@@ -2,7 +2,6 @@
namespace Drupal\Tests\block_field\Functional;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Tests\BrowserTestBase;
/**
......@@ -12,8 +11,6 @@ use Drupal\Tests\BrowserTestBase;
*/
class BlockFieldTest extends BrowserTestBase {
use StringTranslationTrait;
/**
* Modules to enable.
*
......@@ -47,19 +44,19 @@ class BlockFieldTest extends BrowserTestBase {
// Check that add more and ajax callbacks are working as expected.
$this->drupalPostForm('node/add/block_field_test', [
'title[0][value]' => 'Block field test',
], $this->t('Add another item'));
$this->drupalPostForm(NULL, [], $this->t('Add another item'));
], 'Add another item');
$this->drupalPostForm(NULL, [], 'Add another item');
$this->drupalPostForm(NULL, [
'field_block_field_test[0][plugin_id]' => 'block_field_test_authenticated',
'field_block_field_test[1][plugin_id]' => 'block_field_test_content',
'field_block_field_test[2][plugin_id]' => 'block_field_test_time',
], $this->t('Add another item'));
], 'Add another item');
$this->drupalPostForm(NULL, [
'field_block_field_test[0][plugin_id]' => 'block_field_test_authenticated',
'field_block_field_test[1][plugin_id]' => 'block_field_test_content',
'field_block_field_test[2][plugin_id]' => 'block_field_test_time',
], $this->t('Add another item'));
$this->drupalPostForm(NULL, [], $this->t('Save'));
], 'Add another item');
$this->drupalPostForm(NULL, [], 'Save');
// Check blocks displayed to authenticated.
$node = $this->drupalGetNodeByTitle('Block field test');
......@@ -80,7 +77,7 @@ class BlockFieldTest extends BrowserTestBase {
$this->drupalPostForm(NULL, [
'field_block_field_test[0][_weight]' => 1,
'field_block_field_test[1][_weight]' => 0,
], $this->t('Save'));
], 'Save');
$this->drupalGet($node->toUrl('edit-form'));
// Plugin id and label should be switched.
$assert->fieldValueEquals('field_block_field_test[0][plugin_id]', 'block_field_test_content');
......@@ -160,7 +157,7 @@ class BlockFieldTest extends BrowserTestBase {
$assert->responseNotContains($time);
$this->drupalGet('admin/structure/types/manage/block_field_test/fields/node.block_field_test.field_block_field_test');
$this->drupalPostForm(NULL, ['settings[plugin_ids][page_title_block]' => FALSE], $this->t('Save settings'));
$this->drupalPostForm(NULL, ['settings[plugin_ids][page_title_block]' => FALSE], 'Save settings');
$this->drupalGet('admin/structure/types/manage/block_field_test/fields/node.block_field_test.field_block_field_test');
$assert->statusCodeEquals(200);
......
......@@ -2,7 +2,6 @@
namespace Drupal\Tests\block_field\Functional;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Tests\BrowserTestBase;
/**
......@@ -12,8 +11,6 @@ use Drupal\Tests\BrowserTestBase;
*/
class WidgetTest extends BrowserTestBase {
use StringTranslationTrait;
/**
* {@inheritdoc}
*/
......@@ -22,6 +19,7 @@ class WidgetTest extends BrowserTestBase {
'user',
'block',
'block_field',
'block_field_test',
'block_field_widget_test',
'field_ui',
];
......@@ -52,7 +50,7 @@ class WidgetTest extends BrowserTestBase {
$this->drupalPostForm('node/add/block_node', [
'title[0][value]' => 'Block field test',
'field_block[0][plugin_id]' => 'views_block:items-block_1',
], $this->t('Save'));
], 'Save');
$this->blockNode = $this->drupalGetNodeByTitle('Block field test');
}
......@@ -125,7 +123,7 @@ class WidgetTest extends BrowserTestBase {
$edit = [
'fields[field_block][settings_edit_form][settings][configuration_form]' => 'hidden',
];
$this->drupalPostForm(NULL, $edit, t('Save'));
$this->drupalPostForm(NULL, $edit, 'Save');
$this->drupalGet($this->blockNode->toUrl('edit-form'));
$assert->fieldNotExists('field_block[0][settings][label_display]');
$assert->fieldNotExists('field_block[0][settings][override][items_per_page]');
......@@ -133,6 +131,34 @@ class WidgetTest extends BrowserTestBase {
$assert->fieldNotExists('field_block[0][settings][views_label]');
}
/**
* Tests that validation errors from the block form are bubbled up.
*/
public function testBlockFieldValidation() {
$assert = $this->assertSession();
$this->drupalPostForm('node/add/block_node', [
'title[0][value]' => 'Block field validation test',
'field_block[0][plugin_id]' => 'block_field_test_validation',
], 'Save');
$node = $this->drupalGetNodeByTitle('Block field validation test');
$this->drupalGet($node->toUrl('edit-form'));
$this->submitForm([
'field_block[0][settings][content]' => 'error by name',
], 'Save');
$assert->pageTextContains('Come ere boi!');
$assert->elementAttributeContains('css', 'input[name="field_block[0][settings][content]"]', 'class', 'error');
$this->submitForm([
'field_block[0][settings][content]' => 'error by element',
], 'Save');
$assert->pageTextContains('Gimmie them toez!');
$assert->elementAttributeContains('css', 'input[name="field_block[0][settings][content]"]', 'class', 'error');
$this->submitForm([
'field_block[0][settings][content]' => 'something else',
], 'Save');
$assert->pageTextContains('Block node Block field validation test has been updated.');
}
/**
* Create dummy nodes.
*
......
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