diff --git a/core/lib/Drupal/Core/Entity/Entity/EntityFormDisplay.php b/core/lib/Drupal/Core/Entity/Entity/EntityFormDisplay.php index 3791e54..6f01420 100644 --- a/core/lib/Drupal/Core/Entity/Entity/EntityFormDisplay.php +++ b/core/lib/Drupal/Core/Entity/Entity/EntityFormDisplay.php @@ -342,27 +342,8 @@ public function getPluginCollections() { public function calculateDependencies() { parent::calculateDependencies(); - /** @var \Drupal\Core\Field\WidgetPluginManager $widget_manager */ - $manager = \Drupal::service('plugin.manager.field.widget'); - $autocomplete_class = 'Drupal\Core\Field\Plugin\Field\FieldWidget\EntityReferenceAutocompleteWidget'; - - foreach ($this->getComponents() as $name => $info) { - // Take into account only 'entity_reference_autocomplete' and descendants. - $class = $manager->getDefinition($info['type'])['class']; - if ($class != $autocomplete_class && !is_subclass_of($class, $autocomplete_class)) { - continue; - } - - if (!empty($bundle_id = $info['settings']['auto_create_bundle'])) { - $target_type_id = $this->fieldDefinitions[$name]->getSetting('target_type'); - $target_type = \Drupal::entityManager()->getDefinition($target_type_id); - if ($bundle_entity_type = $target_type->getBundleEntityType()) { - $storage = \Drupal::entityManager()->getStorage($bundle_entity_type); - if ($bundle = $storage->load($bundle_id)) { - $this->addDependency($bundle->getConfigDependencyKey(), $bundle->getConfigDependencyName()); - } - } - } + foreach ($this->getEntityReferenceAutocompleteComponents() as $name => $bundle) { + $this->addDependency($bundle->getConfigDependencyKey(), $bundle->getConfigDependencyName()); } return $this->dependencies; @@ -374,34 +355,54 @@ public function calculateDependencies() { public function onDependencyRemoval(array $dependencies) { $changed = parent::onDependencyRemoval($dependencies); + foreach ($this->getEntityReferenceAutocompleteComponents() as $name => $bundle) { + if (!empty($dependencies[$bundle->getConfigDependencyKey()][$bundle->getConfigDependencyName()])) { + $component = $this->getComponent($name); + $component['settings']['auto_create'] = FALSE; + $component['settings']['auto_create_bundle'] = NULL; + $this->setComponent($name, $component); + $changed = TRUE; + } + } + + return $changed; + } + + /** + * Provides a list of components of type 'entity_reference_autocomplete', or + * descendants, that have the 'auto_create_bundle' setting pointing to + * an bundle represented as an existing config entity. + * + * @return \Drupal\Core\Config\Entity\ConfigEntityInterface[] + * Associative array keyed by field name and having the bundle config entity + * object as value. + */ + protected function getEntityReferenceAutocompleteComponents() { /** @var \Drupal\Core\Field\WidgetPluginManager $widget_manager */ $manager = \Drupal::service('plugin.manager.field.widget'); - $autocomplete_class = 'Drupal\Core\Field\Plugin\Field\FieldWidget\EntityReferenceAutocompleteWidget'; + $ancestor = 'Drupal\Core\Field\Plugin\Field\FieldWidget\EntityReferenceAutocompleteWidget'; - foreach ($this->getComponents() as $name => $info) { + $components = []; + foreach ($this->getComponents() as $name => $component) { // Take into account only 'entity_reference_autocomplete' and descendants. - $class = $manager->getDefinition($info['type'])['class']; - if ($class != $autocomplete_class && !is_subclass_of($class, $autocomplete_class)) { + $class = !empty($component['type']) ? $manager->getDefinition($component['type'])['class'] : FALSE; + if (!$class || ($class != $ancestor && !is_subclass_of($class, $ancestor))) { continue; } - if (!empty($bundle_id = $info['settings']['auto_create_bundle'])) { + if (!empty($bundle_id = $component['settings']['auto_create_bundle'])) { $target_type_id = $this->fieldDefinitions[$name]->getSetting('target_type'); $target_type = \Drupal::entityManager()->getDefinition($target_type_id); if ($bundle_entity_type = $target_type->getBundleEntityType()) { $storage = \Drupal::entityManager()->getStorage($bundle_entity_type); - $bundle = $storage->load($bundle_id); - if ($bundle && !empty($dependencies[$bundle->getConfigDependencyKey()][$bundle->getConfigDependencyName()])) { - $info['settings']['auto_create'] = FALSE; - $info['settings']['auto_create_bundle'] = NULL; - $this->setComponent($name, $info); - $changed = TRUE; + if ($bundle = $storage->load($bundle_id)) { + $components[$name] = $bundle; } } } } - return $changed; + return $components; } } diff --git a/core/modules/system/src/Tests/Field/Update/EntityReferenceSettingsUpdateTest.php b/core/modules/system/src/Tests/Field/Update/EntityReferenceSettingsUpdateTest.php new file mode 100644 index 0000000..d5532a6 --- /dev/null +++ b/core/modules/system/src/Tests/Field/Update/EntityReferenceSettingsUpdateTest.php @@ -0,0 +1,65 @@ +databaseDumpFiles = [ + __DIR__ . '/../../../../tests/fixtures/update/drupal-8.bare.standard.php.gz', + ]; + parent::setUp(); + } + + /** + * Tests system_update_8004(). + */ + public function testSystemUpdate8004() { + /** @var \Drupal\Core\Config\ConfigFactoryInterface $factory */ + $factory = $this->container->get('config.factory'); + + $field_config = $factory->get('field.field.node.article.field_tags'); + + // The legacy 'auto_create' storage setting exists and is TRUE. + $this->assertTrue($field_config->get('settings.handler_settings.auto_create')); + + // Run updates. + $this->runUpdates(); + + // Check again after updating. + $field_config = $factory->get('field.field.node.article.field_tags'); + $handler_settings = $field_config->get('settings.handler_settings'); + + // The 'auto_create' setting should no longer exist. + $this->assertFalse(array_key_exists('auto_create', $handler_settings)); + + // Check now the for display widget settings. + $form_display = $factory->get('core.entity_form_display.node.article.default'); + + // 'auto_create' should be migrated here and has to be TRUE. + $this->assertTrue($form_display->get('content.field_tags.settings.auto_create')); + + // 'auto_create_bundle' should be created and has to be NULL. + $this->assertNull($form_display->get('content.field_tags.settings.auto_create_bundle')); + } + +}