diff --git a/core/config/schema/core.data_types.schema.yml b/core/config/schema/core.data_types.schema.yml index 5316b83..44e3078 100644 --- a/core/config/schema/core.data_types.schema.yml +++ b/core/config/schema/core.data_types.schema.yml @@ -773,12 +773,16 @@ 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.' + auto_create_entity: + type: mapping + label: 'Entity auto-creation settings' + mapping: + enabled: + type: boolean + label: 'Create referenced entities if they don''t already exist' + bundle: + type: string + label: 'Bundle assigned to the auto-created entities.' entity_reference_selection.*: type: entity_reference_selection diff --git a/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/SelectionBase.php b/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/SelectionBase.php index f6c374f..c42029b 100644 --- a/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/SelectionBase.php +++ b/core/lib/Drupal/Core/Entity/Plugin/EntityReferenceSelection/SelectionBase.php @@ -115,8 +115,10 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta 'sort' => array( 'field' => '_none', ), - 'auto_create' => FALSE, - 'auto_create_bundle' => NULL, + 'auto_create_entity' => array( + 'enabled' => FALSE, + 'bundle' => NULL, + ), ); if ($entity_type->hasKey('bundle')) { @@ -214,23 +216,28 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta } } - $form['auto_create'] = array( + $form['auto_create_entity'] = array( + '#type' => 'details', + '#title' => $this->t('Auto-create entities'), + '#open' => $selection_handler_settings['auto_create_entity']['enabled'], + ); + $form['auto_create_entity']['enabled'] = array( '#type' => 'checkbox', '#title' => $this->t("Create referenced entities if they don't already exist"), - '#default_value' => $selection_handler_settings['auto_create'], + '#default_value' => $selection_handler_settings['auto_create_entity']['enabled'], ); if ($entity_type->hasKey('bundle')) { $bundles = array_intersect_key($bundle_options, $selection_handler_settings['target_bundles']); - $form['auto_create_bundle'] = array( + $form['auto_create_entity']['bundle'] = array( '#type' => 'select', '#title' => $this->t('Store new items in'), '#options' => $bundles, - '#default_value' => $selection_handler_settings['auto_create_bundle'], + '#default_value' => $selection_handler_settings['auto_create_entity']['bundle'], '#access' => count($bundles) > 1, '#states' => array( 'visible' => array( - ':input[name="field[settings][handler_settings][auto_create]"]' => array('checked' => TRUE), + ':input[name="field[settings][handler_settings][auto_create_entity][enabled]"]' => array('checked' => TRUE), ), ), ); diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceEntityFormatter.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceEntityFormatter.php index 1328f65..d04f141 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceEntityFormatter.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceEntityFormatter.php @@ -143,7 +143,7 @@ public function viewElements(FieldItemListInterface $items) { } } else { - // This is an "auto_create" item. + // This is an 'auto_create_entity' item. $elements[$delta] = array('#markup' => $entity->label()); } $depth = 0; 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 12d5c0a..d3425ee 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/EntityReferenceAutocompleteWidget.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/EntityReferenceAutocompleteWidget.php @@ -108,7 +108,8 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen '#placeholder' => $this->getSetting('placeholder'), ); - if ($this->getSelectionHandlerSetting('auto_create')) { + $auto_create = $this->getSelectionHandlerSetting('auto_create_entity'); + if ($auto_create['enabled']) { $element['#autocreate'] = array( 'bundle' => $this->getAutocreateBundle(), 'uid' => ($entity instanceof EntityOwnerInterface) ? $entity->getOwnerId() : \Drupal::currentUser()->id() @@ -153,13 +154,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')) { + $auto_create = $this->getSelectionHandlerSetting('auto_create_entity'); + if (!empty($auto_create['enabled']) && $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')) { + elseif (!empty($auto_create['bundle'])) { + $bundle = $auto_create['bundle']; + } + else { // If no bundle has been set as auto create target means that there is // an inconsistency in entity reference field settings. throw new \InvalidArgumentException("Reference auto-create is enabled on multiple target bundles but 'auto_create_bundle' is not set."); diff --git a/core/modules/entity_reference/src/Tests/EntityReferenceAdminTest.php b/core/modules/entity_reference/src/Tests/EntityReferenceAdminTest.php index 4616bb2..0a53968 100644 --- a/core/modules/entity_reference/src/Tests/EntityReferenceAdminTest.php +++ b/core/modules/entity_reference/src/Tests/EntityReferenceAdminTest.php @@ -139,7 +139,7 @@ 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('field[settings][handler_settings][auto_create]'); + $this->assertFieldByName('field[settings][handler_settings][auto_create_entity][enabled]'); // Switch the target type to 'user' and check that the settings specific to // its selection handler are displayed. @@ -246,8 +246,8 @@ public function testMultipleTargetBundles() { $this->drupalGet($path); - // Expect that there's no 'auto_create_bundle' selected. - $this->assertNoFieldByName('field[settings][handler_settings][auto_create_bundle]'); + // Expect that there's no bundle selected. + $this->assertNoFieldByName('field[settings][handler_settings][auto_create_entity][bundle]'); $edit = [ 'field[settings][handler_settings][target_bundles][' . $vocabularies[1]->id() . ']' => TRUE, @@ -255,18 +255,18 @@ public function testMultipleTargetBundles() { // 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='field[settings][handler_settings][auto_create_entity][bundle]']/option[@value='" . $vocabularies[0]->id() . "']"); + $this->assertFieldByXPath("//select[@name='field[settings][handler_settings][auto_create_entity][bundle]']/option[@value='" . $vocabularies[1]->id() . "']"); $edit = [ - 'field[settings][handler_settings][auto_create_bundle]' => $vocabularies[1]->id(), + 'field[settings][handler_settings][auto_create_entity][bundle]' => $vocabularies[1]->id(), ]; $this->drupalPostForm(NULL, $edit, t('Save 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->settings['handler_settings']['auto_create_bundle'], $vocabularies[1]->id()); + $this->assertEqual($field_config->settings['handler_settings']['auto_create_entity']['bundle'], $vocabularies[1]->id()); } /** diff --git a/core/modules/entity_reference/src/Tests/EntityReferenceAutoCreateTest.php b/core/modules/entity_reference/src/Tests/EntityReferenceAutoCreateTest.php index 69f6491..1531657 100644 --- a/core/modules/entity_reference/src/Tests/EntityReferenceAutoCreateTest.php +++ b/core/modules/entity_reference/src/Tests/EntityReferenceAutoCreateTest.php @@ -49,43 +49,21 @@ protected function setUp() { $referenced = $this->drupalCreateContentType(); $this->referencedType = $referenced->id(); - entity_create('field_storage_config', array( - 'field_name' => 'test_field', - 'entity_type' => 'node', - 'translatable' => FALSE, - 'entity_types' => array(), - 'settings' => array( - 'target_type' => 'node', - ), - 'type' => 'entity_reference', - 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED, - ))->save(); - - entity_create('field_config', array( - 'label' => 'Entity reference field', - 'field_name' => 'test_field', - 'entity_type' => 'node', - 'bundle' => $referencing->id(), - 'settings' => array( - 'handler' => 'default', - 'handler_settings' => array( - // Reference a single vocabulary. - 'target_bundles' => array( - $referenced->id(), - ), - // Enable auto-create. - 'auto_create' => TRUE, - ), - ), - ))->save(); + $handler_settings = [ + // Reference a single vocabulary. + 'target_bundles' => [$referenced->id() => $referenced->id()], + // Enable auto-create. + 'auto_create_entity' => ['enabled' => TRUE], + ]; + $this->createEntityReferenceField('node', $referencing->id(), 'test_field', 'Entity reference field', 'node', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED); entity_get_display('node', $referencing->id(), 'default') ->setComponent('test_field') ->save(); entity_get_form_display('node', $referencing->id(), 'default') - ->setComponent('test_field', array( + ->setComponent('test_field', [ 'type' => 'entity_reference_autocomplete', - )) + ]) ->save(); $account = $this->drupalCreateUser(['access content', "create $this->referencingType content"]); @@ -165,8 +143,10 @@ public function testMultipleTargetBundles() { $vocabularies[0]->id() => $vocabularies[0]->id(), $vocabularies[1]->id() => $vocabularies[1]->id(), ], - 'auto_create' => TRUE, - 'auto_create_bundle' => $vocabularies[1]->id(), + 'auto_create_entity' => [ + 'enabled' => TRUE, + '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 */ @@ -192,7 +172,7 @@ public function testMultipleTargetBundles() { $field_config = FieldConfig::load('node.' . $this->referencingType . ".$field_name"); // 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(); + $field_config->settings['handler_settings']['auto_create_entity']['bundle'] = $vocabularies[0]->id(); $field_config->save(); $term_name = $this->randomString(); @@ -210,7 +190,7 @@ 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($field_config->settings['handler_settings']['auto_create_entity']['bundle']); $field_config->save(); // If we use $this->drupalGet() we cannot catch the exception because the @@ -220,7 +200,7 @@ public function testMultipleTargetBundles() { /** @var \Drupal\Core\Form\FormBuilderInterface $form_builder */ $form_builder = $this->container->get('entity.form_builder'); $form_builder->getForm(Node::load(1)); - $this->fail("Missed 'auto_create_bundle' should throw \\InvalidArgumentException but it didn't."); + $this->fail("Missed bundle for auto-created entities should throw \\InvalidArgumentException but it didn't."); } catch (\InvalidArgumentException $e) { $this->assertIdentical( diff --git a/core/modules/entity_reference/src/Tests/EntityReferenceTestTrait.php b/core/modules/entity_reference/src/Tests/EntityReferenceTestTrait.php index 1882079..e1241f4 100644 --- a/core/modules/entity_reference/src/Tests/EntityReferenceTestTrait.php +++ b/core/modules/entity_reference/src/Tests/EntityReferenceTestTrait.php @@ -33,7 +33,7 @@ * The selection handler used by this field. * @param array $selection_handler_settings * An array of settings supported by the selection handler specified above. - * (e.g. 'target_bundles', 'sort', 'auto_create', etc). + * (e.g. 'target_bundles', 'sort', 'auto_create_entity', etc). * @param int $cardinality * The cardinality of the field. * diff --git a/core/modules/system/src/Tests/Entity/EntityCacheTagsTestBase.php b/core/modules/system/src/Tests/Entity/EntityCacheTagsTestBase.php index 3b382e1..204240a 100644 --- a/core/modules/system/src/Tests/Entity/EntityCacheTagsTestBase.php +++ b/core/modules/system/src/Tests/Entity/EntityCacheTagsTestBase.php @@ -245,7 +245,7 @@ protected function createReferenceTestEntities($referenced_entity) { $referenced_entity->bundle() => $referenced_entity->bundle(), ), 'sort' => array('field' => '_none'), - 'auto_create' => FALSE, + 'auto_create_entity' => array('enabled' => FALSE), ), ), ))->save();