diff --git a/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/SelectionBase.php b/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/SelectionBase.php index 5545720..f22c914 100644 --- a/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/SelectionBase.php +++ b/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/SelectionBase.php @@ -221,7 +221,7 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta ); if ($entity_type->hasKey('bundle')) { - $bundles = array_intersect_key($bundle_options, $selection_handler_settings['target_bundles']); + $bundles = array_intersect_key($bundle_options, array_filter($selection_handler_settings['target_bundles'])); $form['auto_create_bundle'] = array( '#type' => 'select', '#title' => $this->t('Store new items in'), @@ -230,7 +230,7 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta '#access' => count($bundles) > 1, '#states' => array( 'visible' => array( - ':input[name="field[settings][handler_settings][auto_create]"]' => array('checked' => TRUE), + ':input[name="settings[handler_settings][auto_create]"]' => array('checked' => TRUE), ), ), ); @@ -245,7 +245,7 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta 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']); + $form_state->unsetValue(['settings', 'handler_settings', 'target_bundles_update']); } /** diff --git a/core/modules/entity_reference/src/ConfigurableEntityReferenceItem.php b/core/modules/entity_reference/src/ConfigurableEntityReferenceItem.php index c166bc5..7d1f862 100644 --- a/core/modules/entity_reference/src/ConfigurableEntityReferenceItem.php +++ b/core/modules/entity_reference/src/ConfigurableEntityReferenceItem.php @@ -236,14 +236,18 @@ public static function getPreconfiguredOptions() { */ public static function calculateDependencies(FieldDefinitionInterface $field_definition) { $dependencies = parent::calculateDependencies($field_definition); + $handler_settings = $field_definition->getSetting('handler_settings'); if (!empty($handler_settings['auto_create_bundle'])) { $target_type = \Drupal::entityManager()->getDefinition($field_definition->getFieldStorageDefinition()->getSetting('target_type')); - $bundle = entity_load($target_type->getBundleEntityType(), $handler_settings['auto_create_bundle']); - if ($bundle) { - $dependencies[$bundle->getConfigDependencyKey()][] = $bundle->getConfigDependencyName(); + if ($bundle_entity_type = $target_type->getBundleEntityType()) { + $storage = \Drupal::entityManager()->getStorage($bundle_entity_type); + if ($bundle = $storage->load($handler_settings['auto_create_bundle'])) { + $dependencies[$bundle->getConfigDependencyKey()][] = $bundle->getConfigDependencyName(); + } } } + return $dependencies; } @@ -252,16 +256,22 @@ public static function calculateDependencies(FieldDefinitionInterface $field_def */ public static function onDependencyRemoval(FieldDefinitionInterface $field_definition, array $dependencies) { $changed = parent::onDependencyRemoval($field_definition, $dependencies); + $handler_settings = $field_definition->getSetting('handler_settings'); if (!empty($handler_settings['auto_create_bundle'])) { $target_type = \Drupal::entityManager()->getDefinition($field_definition->getFieldStorageDefinition()->getSetting('target_type')); - $bundle = entity_load($target_type->getBundleEntityType(), $handler_settings['auto_create_bundle']); - if ($bundle && isset($dependencies[$bundle->getConfigDependencyKey()][$bundle->getConfigDependencyName()])) { - unset($field_definition->settings['handler_settings']['auto_create_bundle']); - $field_definition->settings['handler_settings']['auto_create'] = FALSE; - $changed = TRUE; + if ($bundle_entity_type = $target_type->getBundleEntityType()) { + $storage = \Drupal::entityManager()->getStorage($bundle_entity_type); + $bundle = $storage->load($handler_settings['auto_create_bundle']); + if ($bundle && isset($dependencies[$bundle->getConfigDependencyKey()][$bundle->getConfigDependencyName()])) { + unset($handler_settings['auto_create_bundle']); + $handler_settings['auto_create'] = FALSE; + $field_definition->setSetting('handler_settings', $handler_settings); + $changed = TRUE; + } } } + return $changed; } diff --git a/core/modules/entity_reference/src/Tests/EntityReferenceAdminTest.php b/core/modules/entity_reference/src/Tests/EntityReferenceAdminTest.php index acb8f73..ec7382a 100644 --- a/core/modules/entity_reference/src/Tests/EntityReferenceAdminTest.php +++ b/core/modules/entity_reference/src/Tests/EntityReferenceAdminTest.php @@ -388,20 +388,20 @@ public function testMultipleTargetBundles() { $this->drupalGet($path); // Expect that there's no 'auto_create_bundle' selected. - $this->assertNoFieldByName('field[settings][handler_settings][auto_create_bundle]'); + $this->assertNoFieldByName('settings[handler_settings][auto_create_bundle]'); $edit = [ - 'field[settings][handler_settings][target_bundles][' . $vocabularies[1]->id() . ']' => TRUE, + 'settings[handler_settings][target_bundles][' . $vocabularies[1]->id() . ']' => TRUE, ]; // Enable the second vocabulary as a target bundle. $this->drupalPostAjaxForm($path, $edit, key($edit)); // Expect a select element with the two vocabularies as options. - $this->assertFieldByXPath("//select[@name='field[settings][handler_settings][auto_create_bundle]']/option[@value='" . $vocabularies[0]->id() . "']"); - $this->assertFieldByXPath("//select[@name='field[settings][handler_settings][auto_create_bundle]']/option[@value='" . $vocabularies[1]->id() . "']"); + $this->assertFieldByXPath("//select[@name='settings[handler_settings][auto_create_bundle]']/option[@value='" . $vocabularies[0]->id() . "']"); + $this->assertFieldByXPath("//select[@name='settings[handler_settings][auto_create_bundle]']/option[@value='" . $vocabularies[1]->id() . "']"); $edit = [ - 'field[settings][handler_settings][auto_create]' => TRUE, - 'field[settings][handler_settings][auto_create_bundle]' => $vocabularies[1]->id(), + 'settings[handler_settings][auto_create]' => TRUE, + 'settings[handler_settings][auto_create_bundle]' => $vocabularies[1]->id(), ]; $this->drupalPostForm(NULL, $edit, t('Save settings')); diff --git a/core/modules/entity_reference/src/Tests/EntityReferenceAutoCreateTest.php b/core/modules/entity_reference/src/Tests/EntityReferenceAutoCreateTest.php index 69f6491..4f3636e 100644 --- a/core/modules/entity_reference/src/Tests/EntityReferenceAutoCreateTest.php +++ b/core/modules/entity_reference/src/Tests/EntityReferenceAutoCreateTest.php @@ -189,10 +189,13 @@ public function testMultipleTargetBundles() { $this->assertEqual($vocabularies[1]->id(), $term->bundle()); /** @var \Drupal\field\Entity\FieldConfig $field_config */ - $field_config = FieldConfig::load('node.' . $this->referencingType . ".$field_name"); + $field_config = FieldConfig::loadByName('node', $this->referencingType, $field_name); + $handler_settings = $field_config->getSetting('handler_settings'); + // Change the field setting to store the auto-created terms in the first // vocabulary and test again. - $field_config->settings['handler_settings']['auto_create_bundle'] = $vocabularies[0]->id(); + $handler_settings['auto_create_bundle'] = $vocabularies[0]->id(); + $field_config->setSetting('handler_settings', $handler_settings); $field_config->save(); $term_name = $this->randomString(); @@ -210,7 +213,8 @@ public function testMultipleTargetBundles() { $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']); + unset($handler_settings['auto_create_bundle']); + $field_config->setSetting('handler_settings', $handler_settings); $field_config->save(); // If we use $this->drupalGet() we cannot catch the exception because the