diff --git a/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/SelectionBase.php b/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/SelectionBase.php index c2e91c2..f6c374f 100644 --- a/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/SelectionBase.php +++ b/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/SelectionBase.php @@ -243,6 +243,8 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta * {@inheritdoc} */ public function validateConfigurationForm(array &$form, FormStateInterface $form_state) { + // Don't store the 'target_bundles_update' button value into the field + // config settings. $form_state->unsetValue(['field', 'settings', 'handler_settings', 'target_bundles_update']); } diff --git a/core/modules/entity_reference/src/Tests/EntityReferenceAdminTest.php b/core/modules/entity_reference/src/Tests/EntityReferenceAdminTest.php index 7b5a554..4616bb2 100644 --- a/core/modules/entity_reference/src/Tests/EntityReferenceAdminTest.php +++ b/core/modules/entity_reference/src/Tests/EntityReferenceAdminTest.php @@ -223,10 +223,10 @@ public function testAvailableFormatters() { } /** - * Tests field settings for taxonomy term entity reference field when the - * field is set to auto-create the target term in more than one vocabularies. + * Tests field settings for an entity reference field when the field has + * multiple target bundles and is set to auto-create the target entity. */ - public function testMultipleTargetTaxonomyVocabularies() { + public function testMultipleTargetBundles() { /** @var \Drupal\taxonomy\Entity\Vocabulary[] $vocabularies */ $vocabularies = []; for ($i = 0; $i < 2; $i++) { diff --git a/core/modules/entity_reference/src/Tests/EntityReferenceAutoCreateTest.php b/core/modules/entity_reference/src/Tests/EntityReferenceAutoCreateTest.php index a9b389f..69f6491 100644 --- a/core/modules/entity_reference/src/Tests/EntityReferenceAutoCreateTest.php +++ b/core/modules/entity_reference/src/Tests/EntityReferenceAutoCreateTest.php @@ -10,7 +10,6 @@ use Drupal\Component\Utility\Unicode; use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\field\Entity\FieldConfig; -use Drupal\field\Entity\FieldStorageConfig; use Drupal\taxonomy\Entity\Vocabulary; use Drupal\simpletest\WebTestBase; use Drupal\node\Entity\Node; @@ -22,6 +21,8 @@ */ class EntityReferenceAutoCreateTest extends WebTestBase { + use EntityReferenceTestTrait; + public static $modules = ['entity_reference', 'node', 'taxonomy']; /** @@ -140,10 +141,10 @@ public function testAutoCreate() { } /** - * Tests if a taxonomy term entity reference field having multiple target - * vocabularies is storing the term in the right destination. + * Tests if a entity reference field having multiple target bundles is storing + * the auto-created entity in the right destination. */ - public function testMultipleTargetTaxonomyVocabularies() { + public function testMultipleTargetBundles() { /** @var \Drupal\taxonomy\Entity\Vocabulary[] $vocabularies */ $vocabularies = []; for ($i = 0; $i < 2; $i++) { @@ -159,31 +160,16 @@ public function testMultipleTargetTaxonomyVocabularies() { // taxonomy terms in the second vocabulary from the two that were configured // as targets. $field_name = Unicode::strtolower($this->randomMachineName()); - FieldStorageConfig::create([ - 'field_name' => $field_name, - 'entity_type' => 'node', - 'settings' => array( - 'target_type' => 'taxonomy_term', - ), - 'type' => 'entity_reference', - ])->save(); - FieldConfig::create([ - 'label' => $this->randomMachineName(), - 'entity_type' => 'node', - 'bundle' => $this->referencingType, - 'field_name' => $field_name, - 'settings' => [ - 'handler' => 'default', - 'handler_settings' => [ - 'target_bundles' => [ - $vocabularies[0]->id() => $vocabularies[0]->id(), - $vocabularies[1]->id() => $vocabularies[1]->id(), - ], - 'auto_create' => TRUE, - 'auto_create_bundle' => $vocabularies[1]->id(), - ], + $handler_settings = [ + 'target_bundles' => [ + $vocabularies[0]->id() => $vocabularies[0]->id(), + $vocabularies[1]->id() => $vocabularies[1]->id(), ], - ])->save(); + 'auto_create' => TRUE, + 'auto_create_bundle' => $vocabularies[1]->id(), + ]; + $this->createEntityReferenceField('node', $this->referencingType, $field_name, $this->randomString(), 'taxonomy_term', 'default', $handler_settings); + /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $fd */ entity_get_form_display('node', $this->referencingType, 'default') ->setComponent($field_name, ['type' => 'entity_reference_autocomplete']) ->save(); @@ -222,6 +208,26 @@ public function testMultipleTargetTaxonomyVocabularies() { // The second term is expected to be stored in the first vocabulary. $this->assertEqual($vocabularies[0]->id(), $term->bundle()); + + // Test the case when the field config settings are inconsistent. + unset($field_config->settings['handler_settings']['auto_create_bundle']); + $field_config->save(); + + // If we use $this->drupalGet() we cannot catch the exception because the + // form builder runs in other PHP space. Instead we are only building the + // form in order to check if the exception is thrown. + try { + /** @var \Drupal\Core\Form\FormBuilderInterface $form_builder */ + $form_builder = $this->container->get('entity.form_builder'); + $form_builder->getForm(Node::load(1)); + $this->fail("Missed 'auto_create_bundle' should throw \\InvalidArgumentException but it didn't."); + } + catch (\InvalidArgumentException $e) { + $this->assertIdentical( + $e->getMessage(), + "Reference auto-create is enabled on multiple target bundles but 'auto_create_bundle' is not set." + ); + } } }