diff --git a/core/modules/field/field.install b/core/modules/field/field.install index 1860ad8..103f7cd 100644 --- a/core/modules/field/field.install +++ b/core/modules/field/field.install @@ -68,3 +68,39 @@ function field_update_8002() { } } } + +/** + * Populate the new 'auto_create_bundle' setting for entity reference fields. + */ +function field_update_8003() { + $config = \Drupal::configFactory(); + /** @var \Drupal\Core\Field\FieldTypePluginManager $field_type_manager */ + $field_type_manager = \Drupal::service('plugin.manager.field.field_type'); + + // Iterate on all fields. + foreach ($config->listAll('field.field.') as $field_id) { + $field = $config->getEditable($field_id); + $class = $field_type_manager->getPluginClass($field->get('field_type')); + + // Deal only with entity reference fields and descendants. + if ($class == EntityReferenceItem::class || is_subclass_of($class, EntityReferenceItem::class)) { + $handler_settings = $field->get('settings.handler_settings'); + + if (isset($handler_settings['auto_create']) && $handler_settings['auto_create']) { + // If the field can reference multiple bundles, pick the first one + // available in order to replicate the previous behavior. + if (is_array($handler_settings['target_bundles']) && count($handler_settings['target_bundles']) > 1) { + $handler_settings['auto_create_bundle'] = reset($handler_settings['target_bundles']); + } + // Otherwise, we don't know which bundle to use for auto-creation so we + // have to disable the functionality completely. + elseif (!$handler_settings['target_bundles']) { + $handler_settings['auto_create'] = FALSE; + $handler_settings['auto_create_bundle'] = NULL; + } + } + + $field->set('settings.handler_settings', $handler_settings)->save(TRUE); + } + } +} diff --git a/core/modules/field/src/Tests/Update/FieldUpdateTest.php b/core/modules/field/src/Tests/Update/FieldUpdateTest.php index 65ca720..d555f47 100644 --- a/core/modules/field/src/Tests/Update/FieldUpdateTest.php +++ b/core/modules/field/src/Tests/Update/FieldUpdateTest.php @@ -112,6 +112,26 @@ public function testFieldUpdate8002() { } /** + * Tests field_update_8003(). + * + * @see field_update_8003() + */ + public function testFieldUpdate8003() { + // Run updates. + $this->runUpdates(); + + // Check that the new 'auto_create_bundle' setting is populated correctly. + $field = $this->configFactory->get('field.field.node.article.field_ref_autocreate_2412569'); + $handler_settings = $field->get('settings.handler_settings'); + + $expected_target_bundles = ['tags' => 'tags', 'test' => 'test']; + $this->assertEqual($handler_settings['target_bundles'], $expected_target_bundles); + + $this->assertTrue($handler_settings['auto_create']); + $this->assertEqual($handler_settings['auto_create_bundle'], 'tags'); + } + + /** * Asserts that a config depends on 'entity_reference' or not * * @param \Drupal\Core\Config\Config $config diff --git a/core/modules/field/tests/fixtures/update/drupal-8.views_entity_reference_plugins-2429191.php b/core/modules/field/tests/fixtures/update/drupal-8.views_entity_reference_plugins-2429191.php index 90f395b..ca7c51a 100644 --- a/core/modules/field/tests/fixtures/update/drupal-8.views_entity_reference_plugins-2429191.php +++ b/core/modules/field/tests/fixtures/update/drupal-8.views_entity_reference_plugins-2429191.php @@ -26,8 +26,14 @@ ]) ->execute(); -// Configuration for an entity_reference field storage. -$config = Yaml::decode(file_get_contents(__DIR__ . '/field.storage.node.field_ref_views_select_2429191.yml')); +// Configuration for an entity_reference field storage using the View for +// selection. +$field_ref_views_select_2429191 = Yaml::decode(file_get_contents(__DIR__ . '/field.storage.node.field_ref_views_select_2429191.yml')); + +// Configuration for an entity_reference field storage using the auto-create +// feature. +$field_ref_autocreate_2412569 = Yaml::decode(file_get_contents(__DIR__ . '/field.storage.node.field_ref_autocreate_2412569.yml')); + $connection->insert('config') ->fields([ 'collection', @@ -36,8 +42,13 @@ ]) ->values([ 'collection' => '', - 'name' => 'field.storage.' . $config['id'], - 'data' => serialize($config), + 'name' => 'field.storage.' . $field_ref_views_select_2429191['id'], + 'data' => serialize($field_ref_views_select_2429191), + ]) + ->values([ + 'collection' => '', + 'name' => 'field.storage.' . $field_ref_autocreate_2412569['id'], + 'data' => serialize($field_ref_autocreate_2412569), ]) ->execute(); // We need to Update the registry of "last installed" field definitions. @@ -48,7 +59,8 @@ ->execute() ->fetchField(); $installed = unserialize($installed); -$installed['field_ref_views_select_2429191'] = new \Drupal\field\Entity\FieldStorageConfig($config); +$installed['field_ref_views_select_2429191'] = new \Drupal\field\Entity\FieldStorageConfig($field_ref_views_select_2429191); +$installed['field_ref_autocreate_2412569'] = new \Drupal\field\Entity\FieldStorageConfig($field_ref_autocreate_2412569); $connection->update('key_value') ->condition('collection', 'entity.definitions.installed') ->condition('name', 'node.field_storage_definitions') @@ -58,7 +70,11 @@ ->execute(); // Configuration for an entity_reference field using the View for selection. -$config = Yaml::decode(file_get_contents(__DIR__ . '/field.field.node.article.field_ref_views_select_2429191.yml')); +$field_ref_views_select_2429191 = Yaml::decode(file_get_contents(__DIR__ . '/field.field.node.article.field_ref_views_select_2429191.yml')); + +// Configuration for an entity_reference field using the auto-create feature. +$field_ref_autocreate_2412569 = Yaml::decode(file_get_contents(__DIR__ . '/field.field.node.article.field_ref_autocreate_2412569.yml')); + $connection->insert('config') ->fields([ 'collection', @@ -67,7 +83,12 @@ ]) ->values([ 'collection' => '', - 'name' => 'field.field.' . $config['id'], - 'data' => serialize($config), + 'name' => 'field.field.' . $field_ref_views_select_2429191['id'], + 'data' => serialize($field_ref_views_select_2429191), + ]) + ->values([ + 'collection' => '', + 'name' => 'field.field.' . $field_ref_autocreate_2412569['id'], + 'data' => serialize($field_ref_autocreate_2412569), ]) ->execute(); diff --git a/core/modules/field/tests/fixtures/update/field.field.node.article.field_ref_autocreate_2412569.yml b/core/modules/field/tests/fixtures/update/field.field.node.article.field_ref_autocreate_2412569.yml new file mode 100644 index 0000000..2606bf6 --- /dev/null +++ b/core/modules/field/tests/fixtures/update/field.field.node.article.field_ref_autocreate_2412569.yml @@ -0,0 +1,29 @@ +uuid: d6deba8d-073a-4572-a000-ee2a2de94de2 +langcode: en +status: true +dependencies: + config: + - field.storage.node.field_ref_autocreate_2412569 + - node.type.article + - taxonomy.vocabulary.tags + - taxonomy.vocabulary.test +id: node.article.field_ref_autocreate_2412569 +field_name: field_ref_autocreate_2412569 +entity_type: node +bundle: article +label: 'Ref Autocreate 2412569' +description: '' +required: false +translatable: false +default_value: { } +default_value_callback: '' +settings: + handler: 'default:taxonomy_term' + handler_settings: + target_bundles: + tags: tags + test: test + sort: + field: _none + auto_create: true +field_type: entity_reference diff --git a/core/modules/field/tests/fixtures/update/field.storage.node.field_ref_autocreate_2412569.yml b/core/modules/field/tests/fixtures/update/field.storage.node.field_ref_autocreate_2412569.yml new file mode 100644 index 0000000..19e4a6a --- /dev/null +++ b/core/modules/field/tests/fixtures/update/field.storage.node.field_ref_autocreate_2412569.yml @@ -0,0 +1,20 @@ +uuid: 5e4095a3-8f89-4d7a-b222-34bf5240b646 +langcode: en +status: true +dependencies: + module: + - node + - taxonomy +id: node.field_ref_autocreate_2412569 +field_name: field_ref_autocreate_2412569 +entity_type: node +type: entity_reference +settings: + target_type: taxonomy_term +module: core +locked: false +cardinality: 1 +translatable: true +indexes: { } +persist_with_no_fields: false +custom_storage: false