From 01d9799de6ccb45209971bb39b3738c122b5bd5f Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Fri, 12 Dec 2014 17:13:50 +0100 Subject: [PATCH] Issue #2354685 by amateescu, tstoeckler, derhasi: Fatal Error on re-saving required EntityReference field setting --- core/lib/Drupal/Core/Field/FieldItemList.php | 3 +++ .../src/Tests/EntityReferenceAdminTest.php | 7 +++++++ .../field_ui/src/Tests/ManageFieldsTest.php | 18 ++++++++++++++++++ 3 files changed, 28 insertions(+) diff --git a/core/lib/Drupal/Core/Field/FieldItemList.php b/core/lib/Drupal/Core/Field/FieldItemList.php index 27305dcae3a..4b29dd3179d 100644 --- a/core/lib/Drupal/Core/Field/FieldItemList.php +++ b/core/lib/Drupal/Core/Field/FieldItemList.php @@ -334,6 +334,9 @@ class FieldItemList extends ItemList implements FieldItemListInterface { // Extract the submitted value, and validate it. $widget = $this->defaultValueWidget($form_state); $widget->extractFormValues($this, $element, $form_state); + // Force a non-required field definition. + // @see self::defaultValueWidget(). + $this->definition->required = FALSE; $violations = $this->validate(); // Assign reported errors to the correct form element. diff --git a/core/modules/entity_reference/src/Tests/EntityReferenceAdminTest.php b/core/modules/entity_reference/src/Tests/EntityReferenceAdminTest.php index a89606c32fa..2b545972f98 100644 --- a/core/modules/entity_reference/src/Tests/EntityReferenceAdminTest.php +++ b/core/modules/entity_reference/src/Tests/EntityReferenceAdminTest.php @@ -103,11 +103,18 @@ class EntityReferenceAdminTest extends WebTestBase { // Third step: confirm. $this->drupalPostForm(NULL, array( + 'field[required]' => '1', 'field[settings][handler_settings][target_bundles][' . key($bundles) . ']' => key($bundles), ), t('Save settings')); // Check that the field appears in the overview form. $this->assertFieldByXPath('//table[@id="field-overview"]//tr[@id="field-test"]/td[1]', 'Test label', 'Field was created and appears in the overview page.'); + + // Check that the field settings form can be submitted again, even when the + // field is required. + // The first 'Edit' link is for the Body field. + $this->clickLink(t('Edit'), 1); + $this->drupalPostForm(NULL, array(), t('Save settings')); } diff --git a/core/modules/field_ui/src/Tests/ManageFieldsTest.php b/core/modules/field_ui/src/Tests/ManageFieldsTest.php index 8c2adb66610..8379c5f99cb 100644 --- a/core/modules/field_ui/src/Tests/ManageFieldsTest.php +++ b/core/modules/field_ui/src/Tests/ManageFieldsTest.php @@ -359,6 +359,24 @@ class ManageFieldsTest extends WebTestBase { $field = FieldConfig::loadByName('node', $this->type, $field_name); $this->assertEqual($field->default_value, NULL, 'The default value was correctly saved.'); + // Check that the default value can be empty when the field is marked as + // required and can store unlimited values. + $field_storage = FieldStorageConfig::loadByName('node', $field_name); + $field_storage->cardinality = FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED; + $field_storage->save(); + + $this->drupalGet($admin_path); + $edit = array( + 'field[required]' => 1, + ); + $this->drupalPostForm(NULL, $edit, t('Save settings')); + + $this->drupalGet($admin_path); + $this->drupalPostForm(NULL, array(), t('Save settings')); + $this->assertText("Saved $field_name configuration", 'The form was successfully submitted.'); + $field = FieldConfig::loadByName('node', $this->type, $field_name); + $this->assertEqual($field->default_value, NULL, 'The default value was correctly saved.'); + // Check that the default widget is used when the field is hidden. entity_get_form_display($field->entity_type, $field->bundle, 'default') ->removeComponent($field_name)->save();