From 1c1e91112f8823f6679c847446522ae8d7b62010 Mon Sep 17 00:00:00 2001 From: catch <catch@35733.no-reply.drupal.org> Date: Wed, 23 Sep 2015 12:28:52 +0200 Subject: [PATCH] Issue #1140188 by torotil, NROTC_Webmaster, cam8001, agi.novanta, jhedstrom, lnunesbr, kathyh: Fatal errors during or after adding default values for autocomplete widgets --- .../Field/EntityReferenceFieldItemList.php | 8 +++++- .../src/Tests/EntityReferenceAdminTest.php | 26 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/core/lib/Drupal/Core/Field/EntityReferenceFieldItemList.php b/core/lib/Drupal/Core/Field/EntityReferenceFieldItemList.php index ee45c7d6ad99..3d735dc367f7 100644 --- a/core/lib/Drupal/Core/Field/EntityReferenceFieldItemList.php +++ b/core/lib/Drupal/Core/Field/EntityReferenceFieldItemList.php @@ -104,7 +104,13 @@ public function defaultValuesFormSubmit(array $element, array &$form, FormStateI // Convert numeric IDs to UUIDs to ensure config deployability. $ids = array(); foreach ($default_value as $delta => $properties) { - $ids[] = $properties['target_id']; + if (isset($properties['entity']) && $properties['entity']->isNew()) { + // This may be a newly created term. + $properties['entity']->save(); + $default_value[$delta]['target_id'] = $properties['entity']->id(); + unset($default_value[$delta]['entity']); + } + $ids[] = $default_value[$delta]['target_id']; } $entities = \Drupal::entityManager() ->getStorage($this->getSetting('target_type')) diff --git a/core/modules/entity_reference/src/Tests/EntityReferenceAdminTest.php b/core/modules/entity_reference/src/Tests/EntityReferenceAdminTest.php index 075ce33226c7..2b341b875a6f 100644 --- a/core/modules/entity_reference/src/Tests/EntityReferenceAdminTest.php +++ b/core/modules/entity_reference/src/Tests/EntityReferenceAdminTest.php @@ -297,6 +297,32 @@ public function testFieldAdminHandler() { ); $this->drupalPostForm('node/add/' . $this->type, $edit, t('Save')); $this->assertLink($node1->getTitle()); + + // Tests adding default values to autocomplete widgets. + Vocabulary::create(array('vid' => 'tags', 'name' => 'tags'))->save(); + $taxonomy_term_field_name = $this->createEntityReferenceField('taxonomy_term', 'tags'); + $field_path = 'node.' . $this->type . '.field_' . $taxonomy_term_field_name; + $this->drupalGet($bundle_path . '/fields/' . $field_path . '/storage'); + $edit = [ + 'cardinality' => -1, + ]; + $this->drupalPostForm(NULL, $edit, t('Save field settings')); + $this->drupalGet($bundle_path . '/fields/' . $field_path); + $term_name = $this->randomString(); + $edit = [ + // This must be set before new entities will be auto-created. + 'settings[handler_settings][auto_create]' => 1, + ]; + $this->drupalPostForm(NULL, $edit, t('Save settings')); + $this->drupalGet($bundle_path . '/fields/' . $field_path); + $edit = [ + // A term that doesn't yet exist. + 'default_value_input[field_' . $taxonomy_term_field_name . '][0][target_id]' => $term_name, + ]; + $this->drupalPostForm(NULL, $edit, t('Save settings')); + // The term should now exist. + $term = taxonomy_term_load_multiple_by_name($term_name, 'tags')[1]; + $this->assertIdentical(1, count($term), 'Taxonomy term was auto created when set as field default.'); } /** -- GitLab