diff --git a/core/config/schema/core.data_types.schema.yml b/core/config/schema/core.data_types.schema.yml index c725792..04e7582 100644 --- a/core/config/schema/core.data_types.schema.yml +++ b/core/config/schema/core.data_types.schema.yml @@ -774,12 +774,6 @@ entity_reference_selection: direction: type: string label: 'Sort direction' - auto_create: - type: boolean - label: 'Create referenced entities if they don''t already exist' - auto_create_bundle: - type: string - label: 'Bundle assigned to the auto-created entities.' entity_reference_selection.*: type: entity_reference_selection diff --git a/core/config/schema/core.entity.schema.yml b/core/config/schema/core.entity.schema.yml index cc1a57e..17ccada 100644 --- a/core/config/schema/core.entity.schema.yml +++ b/core/config/schema/core.entity.schema.yml @@ -213,6 +213,12 @@ field.widget.settings.entity_reference_autocomplete_tags: placeholder: type: label label: 'Placeholder' + auto_create: + type: boolean + label: 'Create referenced entities if they don''t already exist' + auto_create_bundle: + type: string + label: 'Bundle assigned to the auto-created entities.' field.widget.settings.entity_reference_autocomplete: type: mapping @@ -227,6 +233,12 @@ field.widget.settings.entity_reference_autocomplete: placeholder: type: label label: 'Placeholder' + auto_create: + type: boolean + label: 'Create referenced entities if they don''t already exist' + auto_create_bundle: + type: string + label: 'Bundle assigned to the auto-created entities.' field.formatter.settings.boolean: type: mapping diff --git a/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/SelectionBase.php b/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/SelectionBase.php index f22c914..2324fc7 100644 --- a/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/SelectionBase.php +++ b/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/SelectionBase.php @@ -115,8 +115,6 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta 'sort' => array( 'field' => '_none', ), - 'auto_create' => FALSE, - 'auto_create_bundle' => NULL, ); if ($entity_type->hasKey('bundle')) { @@ -129,23 +127,11 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta '#type' => 'checkboxes', '#title' => $this->t('Bundles'), '#options' => $bundle_options, - '#default_value' => $selection_handler_settings['target_bundles'], + '#default_value' => (!empty($selection_handler_settings['target_bundles'])) ? $selection_handler_settings['target_bundles'] : array(), '#required' => TRUE, '#size' => 6, '#multiple' => TRUE, '#element_validate' => array('_entity_reference_element_validate_filter'), - '#ajax' => TRUE, - '#limit_validation_errors' => array(), - ); - - $form['target_bundles_update'] = array( - '#type' => 'submit', - '#value' => $this->t('Update bundles'), - '#limit_validation_errors' => array(), - '#attributes' => array( - 'class' => array('js-hide'), - ), - '#submit' => array('entity_reference_settings_ajax_submit'), ); } else { @@ -214,39 +200,13 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta } } - $form['auto_create'] = array( - '#type' => 'checkbox', - '#title' => $this->t("Create referenced entities if they don't already exist"), - '#default_value' => $selection_handler_settings['auto_create'], - ); - - if ($entity_type->hasKey('bundle')) { - $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'), - '#options' => $bundles, - '#default_value' => $selection_handler_settings['auto_create_bundle'], - '#access' => count($bundles) > 1, - '#states' => array( - 'visible' => array( - ':input[name="settings[handler_settings][auto_create]"]' => array('checked' => TRUE), - ), - ), - ); - } - return $form; } /** * {@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(['settings', 'handler_settings', 'target_bundles_update']); - } + public function validateConfigurationForm(array &$form, FormStateInterface $form_state) { } /** * {@inheritdoc} diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/EntityReferenceAutocompleteWidget.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/EntityReferenceAutocompleteWidget.php index d592062..2c3803c 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/EntityReferenceAutocompleteWidget.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/EntityReferenceAutocompleteWidget.php @@ -35,6 +35,8 @@ public static function defaultSettings() { 'match_operator' => 'CONTAINS', 'size' => '60', 'placeholder' => '', + 'auto_create' => FALSE, + 'auto_create_bundle' => NULL, ) + parent::defaultSettings(); } @@ -42,6 +44,9 @@ public static function defaultSettings() { * {@inheritdoc} */ public function settingsForm(array $form, FormStateInterface $form_state) { + $entity_type_id = $this->getFieldSetting('target_type'); + $entity_type = \Drupal::entityManager()->getDefinition($entity_type_id); + $element['match_operator'] = array( '#type' => 'radios', '#title' => t('Autocomplete matching'), @@ -62,6 +67,35 @@ public function settingsForm(array $form, FormStateInterface $form_state) { '#default_value' => $this->getSetting('placeholder'), '#description' => t('Text that will be shown inside the field until a value is entered. This hint is usually a sample value or a brief description of the expected format.'), ); + $element['auto_create'] = array( + '#type' => 'checkbox', + '#title' => $this->t("Create referenced entities if they don't already exist"), + '#default_value' => $this->getSetting('auto_create'), + ); + + $bundles = $this->getSelectionHandlerSetting('target_bundles'); + if ($entity_type->hasKey('bundle') && $bundles && count($bundles) > 1) { + $options = array(); + foreach (\Drupal::entityManager()->getBundleInfo($entity_type_id) as $id => $info) { + if (in_array($id, $bundles)) { + $options[$id] = $info['label']; + } + } + + $selector_name = 'fields[' . $this->fieldDefinition->getName() . '][settings_edit_form][settings][auto_create]'; + $element['auto_create_bundle'] = array( + '#type' => 'select', + '#title' => $this->t('Store new items in'), + '#options' => $options, + '#default_value' => $this->getSetting('auto_create_bundle'), + '#states' => array( + 'visible' => array( + ':input[name="' . $selector_name . '"]' => array('checked' => TRUE), + ), + ), + ); + } + return $element; } @@ -82,6 +116,24 @@ public function settingsSummary() { $summary[] = t('No placeholder'); } + if ($this->getSetting('auto_create')) { + $entity_type_id = $this->getFieldSetting('target_type'); + $entity_type = \Drupal::entityManager()->getDefinition($entity_type_id); + $bundle_id = $this->getSetting('auto_create_bundle'); + if ($bundle_id) { + $bundle_type = $entity_type->getBundleEntityType(); + $storage = \Drupal::entityManager()->getStorage($bundle_type); + $bundle = $storage->load($bundle_id); + $summary[] = $this->t('Auto-create the %entity in %bundle, if is not in selection list', ['%entity' => $entity_type->getLabel(), '%bundle' => $bundle->label()]); + } + else { + $summary[] = $this->t('Auto-create %entity, if is not in selection list', ['%entity' => $entity_type->getLabel()]); + } + } + else { + $summary[] = $this->t('No entity auto-creation. Selection limited to list.'); + } + return $summary; } @@ -106,7 +158,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen '#placeholder' => $this->getSetting('placeholder'), ); - if ($this->getSelectionHandlerSetting('auto_create')) { + if ($this->getSetting('auto_create')) { $element['#autocreate'] = array( 'bundle' => $this->getAutocreateBundle(), 'uid' => ($entity instanceof EntityOwnerInterface) ? $entity->getOwnerId() : \Drupal::currentUser()->id() @@ -151,17 +203,17 @@ public function massageFormValues(array $values, array $form, FormStateInterface */ protected function getAutocreateBundle() { $bundle = NULL; - if ($this->getSelectionHandlerSetting('auto_create') && $target_bundles = $this->getSelectionHandlerSetting('target_bundles')) { + if ($this->getSetting('auto_create') && $target_bundles = $this->getSelectionHandlerSetting('target_bundles')) { // If there's only one target bundle, use it. if (count($target_bundles) == 1) { $bundle = reset($target_bundles); } - // Otherwise use the target bundle stored in selection handler settings. - elseif (!$bundle = $this->getSelectionHandlerSetting('auto_create_bundle')) { + // Otherwise use the target bundle stored in widget settings. + elseif (!$bundle = $this->getSetting('auto_create_bundle')) { // If no bundle has been set as auto create target means that there is // an inconsistency in entity reference field settings. throw new \InvalidArgumentException(sprintf( - "Create referenced entities if they don't already exist option is enabled but a specific destination bundle is not set. You should re-visit and fix the settings of the '%s' (%s) field.", + "Create referenced entities if they don't already exist option is enabled but a specific destination bundle is not set. You should re-visit 'Manage form display' and fix the widget settings of the '%s' (%s) field.", $this->fieldDefinition->getLabel(), $this->fieldDefinition->getName() )); diff --git a/core/modules/entity_reference/src/ConfigurableEntityReferenceItem.php b/core/modules/entity_reference/src/ConfigurableEntityReferenceItem.php index 0a5ead8..be11617 100644 --- a/core/modules/entity_reference/src/ConfigurableEntityReferenceItem.php +++ b/core/modules/entity_reference/src/ConfigurableEntityReferenceItem.php @@ -11,7 +11,6 @@ use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem; use Drupal\Core\Field\PreconfiguredFieldUiOptionsInterface; -use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\OptGroup; use Drupal\Core\Session\AccountInterface; @@ -231,70 +230,4 @@ public static function getPreconfiguredOptions() { return $options; } - /** - * {@inheritdoc} - */ - public static function calculateDependencies(FieldDefinitionInterface $field_definition) { - $dependencies = parent::calculateDependencies($field_definition); - - $handler_settings = $field_definition->getSetting('handler_settings'); - // Dependencies for 'target_bundles' covers also the 'auto_create_bundle' - // value, if any, because the latter is included in 'target_bundles' list. - if (!empty($handler_settings['target_bundles'])) { - $target_type = \Drupal::entityManager()->getDefinition($field_definition->getFieldStorageDefinition()->getSetting('target_type')); - if ($bundle_entity_type = $target_type->getBundleEntityType()) { - $storage = \Drupal::entityManager()->getStorage($bundle_entity_type); - foreach ($handler_settings['target_bundles'] as $id) { - if ($bundle = $storage->load($id)) { - $dependencies[$bundle->getConfigDependencyKey()][] = $bundle->getConfigDependencyName(); - } - } - } - } - - return $dependencies; - } - - /** - * {@inheritdoc} - */ - public static function onDependencyRemoval(FieldDefinitionInterface $field_definition, array $dependencies) { - $changed = parent::onDependencyRemoval($field_definition, $dependencies); - - $handler_settings = $field_definition->getSetting('handler_settings'); - // Dependencies for 'target_bundles' covers also the 'auto_create_bundle' - // value, if any, because the latter is included in 'target_bundles' list. - if (!empty($handler_settings['target_bundles'])) { - $target_type = \Drupal::entityManager()->getDefinition($field_definition->getFieldStorageDefinition()->getSetting('target_type')); - if ($bundle_entity_type = $target_type->getBundleEntityType()) { - $storage = \Drupal::entityManager()->getStorage($bundle_entity_type); - $auto_create_bundle = !empty($handler_settings['auto_create_bundle']) ? $handler_settings['auto_create_bundle'] : FALSE; - foreach ($handler_settings['target_bundles'] as $id) { - $bundle = $storage->load($id); - if ($bundle && !empty($dependencies[$bundle->getConfigDependencyKey()][$bundle->getConfigDependencyName()])) { - // Remove this bundle from target bundles. - unset($handler_settings['target_bundles'][$id]); - // If this bundle is set also as 'auto_create_bundle' make it NULL. - if ($auto_create_bundle && $auto_create_bundle == $id) { - $handler_settings['auto_create_bundle'] = NULL; - } - $changed = TRUE; - } - } - - // If only one target bundle has left, set that as 'auto_create_bundle'. - if (!empty($handler_settings['auto_create']) && count($handler_settings['target_bundles']) === 1) { - $handler_settings['auto_create_bundle'] = key($handler_settings['target_bundles']); - } - - // If there's no 'auto_create_bundle' set, there's no auto creation. - $handler_settings['auto_create'] = !empty($handler_settings['auto_create_bundle']); - - $field_definition->setSetting('handler_settings', $handler_settings); - } - } - - return $changed; - } - } diff --git a/core/modules/entity_reference/src/Tests/EntityReferenceAdminTest.php b/core/modules/entity_reference/src/Tests/EntityReferenceAdminTest.php index ec7382a..e81612f 100644 --- a/core/modules/entity_reference/src/Tests/EntityReferenceAdminTest.php +++ b/core/modules/entity_reference/src/Tests/EntityReferenceAdminTest.php @@ -8,8 +8,8 @@ namespace Drupal\entity_reference\Tests; use Drupal\Component\Utility\Unicode; -use Drupal\field\Entity\FieldConfig; use Drupal\Core\Entity\Entity; +use Drupal\Core\Entity\Entity\EntityFormDisplay; use Drupal\field_ui\Tests\FieldUiTestTrait; use Drupal\node\Entity\Node; use Drupal\simpletest\WebTestBase; @@ -59,6 +59,7 @@ protected function setUp() { 'access content', 'administer node fields', 'administer node display', + 'administer node form display', 'administer views', 'create ' . $type_name . ' content', 'edit own ' . $type_name . ' content', @@ -148,7 +149,6 @@ public function testFieldAdminHandler() { ); $this->drupalPostForm($bundle_path . '/fields/' . $field_name . '/storage', $edit, t('Save field settings')); $this->drupalGet($bundle_path . '/fields/' . $field_name); - $this->assertFieldByName('settings[handler_settings][auto_create]'); // Switch the target type to 'user' and check that the settings specific to // its selection handler are displayed. @@ -383,45 +383,61 @@ public function testMultipleTargetBundles() { $field_name = $this->createEntityReferenceField('taxonomy_term', [$vocabularies[0]->id()]); $field_name = "field_$field_name"; $field_id = 'node.' . $this->type . '.' . $field_name; - $path = 'admin/structure/types/manage/' . $this->type . '/fields/' . $field_id; + $form_display_path = 'admin/structure/types/manage/' . $this->type . '/form-display'; + $triggering_element = $field_name . '_settings_edit'; - $this->drupalGet($path); + // Set the widget of this field to 'entity_reference_autocomplete_tags'. + $this->drupalPostForm($form_display_path, ["fields[$field_name][type]" => 'entity_reference_autocomplete_tags'], t('Save')); + + // Open the widget settings for the created field. + $this->drupalPostAjaxForm(NULL, [], $triggering_element); // Expect that there's no 'auto_create_bundle' selected. - $this->assertNoFieldByName('settings[handler_settings][auto_create_bundle]'); + $this->assertNoFieldByName("fields[$field_name][settings_edit_form][settings][auto_create_bundle]"); + // Go to field settings and enable the second vocabulary. + $field_settings_path = 'admin/structure/types/manage/' . $this->type . '/fields/' . $field_id; $edit = [ '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='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() . "']"); + $this->drupalPostForm($field_settings_path, $edit, t('Save settings')); + + // Go back to widget settings. + $this->drupalPostAjaxForm($form_display_path, [], $triggering_element); + + $this->assertFieldByXPath("//select[@name='fields[$field_name][settings_edit_form][settings][auto_create_bundle]']/option[@value='" . $vocabularies[0]->id() . "']"); + $this->assertFieldByXPath("//select[@name='fields[$field_name][settings_edit_form][settings][auto_create_bundle]']/option[@value='" . $vocabularies[1]->id() . "']"); $edit = [ - 'settings[handler_settings][auto_create]' => TRUE, - 'settings[handler_settings][auto_create_bundle]' => $vocabularies[1]->id(), + "fields[$field_name][settings_edit_form][settings][auto_create]" => TRUE, + "fields[$field_name][settings_edit_form][settings][auto_create_bundle]" => $vocabularies[1]->id(), ]; - $this->drupalPostForm(NULL, $edit, t('Save settings')); + $this->drupalPostForm(NULL, $edit, t('Update')); + // Submit also the main form. + $this->drupalPostForm(NULL, [], t('Save')); + + /** @var \Drupal\Core\Entity\Display\EntityDisplayInterface $form_display */ + $form_display = EntityFormDisplay::load("node.{$this->type}.default"); + $settings = $form_display->getComponent($field_name)['settings']; - /** @var \Drupal\field\Entity\FieldConfig $field_config */ - $field_config = FieldConfig::load($field_id); // Expect that the target bundle has been saved in the backend. - $this->assertEqual($field_config->getSetting('handler_settings')['auto_create_bundle'], $vocabularies[1]->id()); + $this->assertEqual($settings['auto_create_bundle'], $vocabularies[1]->id()); // Delete the other bundle. Field config should not be affected. $vocabularies[0]->delete(); - $field_config = FieldConfig::load($field_id); - $this->assertTrue($field_config->getSetting('handler_settings')['auto_create']); - $this->assertIdentical($field_config->getSetting('handler_settings')['auto_create_bundle'], $vocabularies[1]->id()); + $form_display = EntityFormDisplay::load("node.{$this->type}.default"); + $settings = $form_display->getComponent($field_name)['settings']; + $this->assertTrue($settings['auto_create']); + $this->assertIdentical($settings['auto_create_bundle'], $vocabularies[1]->id()); // Delete the bundle set for entity auto-creation. Auto-created settings // should be reset (no auto-creation). - $vocabularies[1]->delete(); - $field_config = FieldConfig::load($field_id); - $this->assertFalse($field_config->getSetting('handler_settings')['auto_create']); - $this->assertFalse(isset($field_config->getSetting('handler_settings')['auto_create_bundle'])); + // @todo Implement this once https://www.drupal.org/node/2553169 is in. + //$vocabularies[1]->delete(); + //$form_display = EntityFormDisplay::load("node.{$this->type}.default"); + //$settings = $form_display->getComponent($field_name)['settings']; + //$this->assertFalse($settings['auto_create']); + //$this->assertNull($settings['auto_create_bundle']); } /** diff --git a/core/modules/entity_reference/src/Tests/EntityReferenceAutoCreateTest.php b/core/modules/entity_reference/src/Tests/EntityReferenceAutoCreateTest.php index e9489e5..36364ca 100644 --- a/core/modules/entity_reference/src/Tests/EntityReferenceAutoCreateTest.php +++ b/core/modules/entity_reference/src/Tests/EntityReferenceAutoCreateTest.php @@ -73,8 +73,6 @@ protected function setUp() { 'target_bundles' => array( $referenced->id(), ), - // Enable auto-create. - 'auto_create' => TRUE, ), ), ))->save(); @@ -85,6 +83,7 @@ protected function setUp() { entity_get_form_display('node', $referencing->id(), 'default') ->setComponent('test_field', array( 'type' => 'entity_reference_autocomplete', + 'settings' => array('auto_create' => TRUE), )) ->save(); @@ -145,6 +144,9 @@ public function testAutoCreate() { * the auto-created entity in the right destination. */ public function testMultipleTargetBundles() { + /** @var \Drupal\Core\Config\ConfigFactoryInterface $factory */ + $factory = $this->container->get('config.factory'); + /** @var \Drupal\taxonomy\Entity\Vocabulary[] $vocabularies */ $vocabularies = []; for ($i = 0; $i < 2; $i++) { @@ -165,13 +167,17 @@ public function testMultipleTargetBundles() { $vocabularies[0]->id() => $vocabularies[0]->id(), $vocabularies[1]->id() => $vocabularies[1]->id(), ], - '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']) + ->setComponent($field_name, [ + 'type' => 'entity_reference_autocomplete', + 'settings' => [ + 'auto_create' => TRUE, + 'auto_create_bundle' => $vocabularies[1]->id(), + ], + ]) ->save(); $term_name = $this->randomString(); @@ -194,9 +200,9 @@ public function testMultipleTargetBundles() { // Change the field setting to store the auto-created terms in the first // vocabulary and test again. - $handler_settings['auto_create_bundle'] = $vocabularies[0]->id(); - $field_config->setSetting('handler_settings', $handler_settings); - $field_config->save(); + $factory->getEditable("core.entity_form_display.node.{$this->referencingType}.default") + ->set("content.$field_name.settings.auto_create_bundle", $vocabularies[0]->id()) + ->save(); $term_name = $this->randomString(); $edit = [ @@ -213,9 +219,9 @@ public function testMultipleTargetBundles() { $this->assertEqual($vocabularies[0]->id(), $term->bundle()); // Test the case when the field config settings are inconsistent. - unset($handler_settings['auto_create_bundle']); - $field_config->setSetting('handler_settings', $handler_settings); - $field_config->save(); + $factory->getEditable("core.entity_form_display.node.{$this->referencingType}.default") + ->clear("content.$field_name.settings.auto_create_bundle") + ->save(TRUE); // 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 @@ -230,7 +236,7 @@ public function testMultipleTargetBundles() { $this->assertIdentical( $e->getMessage(), sprintf( - "Create referenced entities if they don't already exist option is enabled but a specific destination bundle is not set. You should re-visit and fix the settings of the '%s' (%s) field.", + "Create referenced entities if they don't already exist option is enabled but a specific destination bundle is not set. You should re-visit 'Manage form display' and fix the widget settings of the '%s' (%s) field.", $field_config->getLabel(), $field_config->getName() ) diff --git a/core/modules/entity_reference/src/Tests/EntityReferenceSettingsTest.php b/core/modules/entity_reference/src/Tests/EntityReferenceSettingsTest.php deleted file mode 100644 index 3a59b14..0000000 --- a/core/modules/entity_reference/src/Tests/EntityReferenceSettingsTest.php +++ /dev/null @@ -1,127 +0,0 @@ -installEntitySchema('node'); - $this->installEntitySchema('taxonomy_term'); - - $this->nodeType = Unicode::strtolower($this->randomMachineName()); - NodeType::create([ - 'type' => $this->nodeType, - 'name' => $this->randomString(), - ])->save(); - - // Create 3 taxonomy vocabularies. - $this->vids = ['vid1', 'vid2', 'vid3']; - $this->vids = array_combine($this->vids, $this->vids); - foreach ($this->vids as $vid) { - Vocabulary::create([ - 'vid' => $vid, - 'name' => $this->randomString(), - ])->save(); - } - } - - /** - * Tests dependencies. - */ - public function testDependency() { - /** @var \Drupal\Core\Config\ConfigFactoryInterface $factory */ - $factory = $this->container->get('config.factory'); - - $name = Unicode::strtolower($this->randomMachineName()); - $label = $this->randomString(); - $handler_settings = [ - 'target_bundles' => $this->vids, - 'auto_create' => TRUE, - 'auto_create_bundle' => 'vid1', - ]; - - // Create an entity reference field with auto creation, targeting 3 taxonomy - // vocabularies. Set the first vocabulary as 'auto_create_bundle'. - $this->createEntityReferenceField('node', $this->nodeType, $name, $label, 'taxonomy_term', 'default', $handler_settings); - $id = "field.field.node.{$this->nodeType}.$name"; - - // Test field creation. - $config = $factory->get($id); - $this->assertIdentical($config->get('settings.handler_settings.target_bundles'), $this->vids); - $this->assertTrue($config->get('settings.handler_settings.auto_create')); - $this->assertIdentical($config->get('settings.handler_settings.auto_create_bundle'), 'vid1'); - - // Delete the first vocabulary. - Vocabulary::load('vid1')->delete(); - - // Reload the field config and test again. - $config = $factory->getEditable($id); - - $this->assertIdentical($config->get('settings.handler_settings.target_bundles'), ['vid2' => 'vid2', 'vid3' => 'vid3']); - // 'auto_create' is FALSE. - $this->assertFalse($config->get('settings.handler_settings.auto_create')); - // 'auto_create_bundle' is NULL. - $this->assertNull($config->get('settings.handler_settings.auto_create_bundle')); - - // Set the 'auto_create_bundle' to 'vid2', 'auto_create' to TRUE. - $config - ->set('settings.handler_settings.auto_create', TRUE) - ->set('settings.handler_settings.auto_create_bundle', 'vid2') - ->save(); - - // Delete the second vocabulary. - Vocabulary::load('vid2')->delete(); - - // Reload the field config and test again. - $config = $factory->get($id); - - $this->assertIdentical($config->get('settings.handler_settings.target_bundles'), ['vid3' => 'vid3']); - // 'auto_create' is TRUE. - $this->assertTrue($config->get('settings.handler_settings.auto_create')); - // 'auto_create_bundle' was automatically moved to 'vid3'. - $this->assertIdentical($config->get('settings.handler_settings.auto_create_bundle'), 'vid3'); - } - -} diff --git a/core/modules/rdf/src/Tests/EntityReferenceFieldAttributesTest.php b/core/modules/rdf/src/Tests/EntityReferenceFieldAttributesTest.php index 52c4e86..0890af3 100644 --- a/core/modules/rdf/src/Tests/EntityReferenceFieldAttributesTest.php +++ b/core/modules/rdf/src/Tests/EntityReferenceFieldAttributesTest.php @@ -51,7 +51,6 @@ protected function setUp() { 'target_bundles' => array( $this->vocabulary->id() => $this->vocabulary->id(), ), - 'auto_create' => TRUE, ); $this->createEntityReferenceField('node', 'article', $this->fieldName, 'Tags', 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);