Commit a547ae61 authored by catch's avatar catch

Issue #2412569 by claudiu.cristea, amateescu, yched, jibran, epari.siva: Allow...

Issue #2412569 by claudiu.cristea, amateescu, yched, jibran, epari.siva: Allow setting the auto-create bundle on entity reference fields with multiple target bundles
parent 19937298
...@@ -795,6 +795,9 @@ entity_reference_selection: ...@@ -795,6 +795,9 @@ entity_reference_selection:
auto_create: auto_create:
type: boolean type: boolean
label: 'Create referenced entities if they don''t already exist' 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.*: entity_reference_selection.*:
type: entity_reference_selection type: entity_reference_selection
...@@ -122,6 +122,7 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta ...@@ -122,6 +122,7 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
'field' => '_none', 'field' => '_none',
), ),
'auto_create' => FALSE, 'auto_create' => FALSE,
'auto_create_bundle' => NULL,
); );
if ($entity_type->hasKey('bundle')) { if ($entity_type->hasKey('bundle')) {
...@@ -139,7 +140,19 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta ...@@ -139,7 +140,19 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
'#size' => 6, '#size' => 6,
'#multiple' => TRUE, '#multiple' => TRUE,
'#element_validate' => [[get_class($this), 'elementValidateFilter']], '#element_validate' => [[get_class($this), 'elementValidateFilter']],
'#ajax' => TRUE,
'#limit_validation_errors' => [],
); );
$form['target_bundles_update'] = [
'#type' => 'submit',
'#value' => $this->t('Update form'),
'#limit_validation_errors' => [],
'#attributes' => [
'class' => ['js-hide'],
],
'#submit' => [[EntityReferenceItem::class, 'settingsAjaxSubmit']],
];
} }
else { else {
$form['target_bundles'] = array( $form['target_bundles'] = array(
...@@ -207,6 +220,30 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta ...@@ -207,6 +220,30 @@ 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'],
'#weight' => -2,
);
if ($entity_type->hasKey('bundle')) {
$bundles = array_intersect_key($bundle_options, array_filter((array) $selection_handler_settings['target_bundles']));
$form['auto_create_bundle'] = [
'#type' => 'select',
'#title' => $this->t('Store new items in'),
'#options' => $bundles,
'#default_value' => $selection_handler_settings['auto_create_bundle'],
'#access' => count($bundles) > 1,
'#states' => [
'visible' => [
':input[name="settings[handler_settings][auto_create]"]' => ['checked' => TRUE],
],
],
'#weight' => -1,
];
}
return $form; return $form;
} }
...@@ -221,6 +258,10 @@ public function validateConfigurationForm(array &$form, FormStateInterface $form ...@@ -221,6 +258,10 @@ public function validateConfigurationForm(array &$form, FormStateInterface $form
if ($form_state->getValue(['settings', 'handler_settings', 'target_bundles']) === []) { if ($form_state->getValue(['settings', 'handler_settings', 'target_bundles']) === []) {
$form_state->setValue(['settings', 'handler_settings', 'target_bundles'], NULL); $form_state->setValue(['settings', 'handler_settings', 'target_bundles'], NULL);
} }
// Don't store the 'target_bundles_update' button value into the field
// config settings.
$form_state->unsetValue(['settings', 'handler_settings', 'target_bundles_update']);
} }
/** /**
......
...@@ -413,7 +413,9 @@ public static function calculateDependencies(FieldDefinitionInterface $field_def ...@@ -413,7 +413,9 @@ public static function calculateDependencies(FieldDefinitionInterface $field_def
} }
} }
// Depend on target bundle configurations. // Depend on target bundle configurations. Dependencies for 'target_bundles'
// also covers the 'auto_create_bundle' setting, if any, because its value
// is included in the 'target_bundles' list.
$handler = $field_definition->getSetting('handler_settings'); $handler = $field_definition->getSetting('handler_settings');
if (!empty($handler['target_bundles'])) { if (!empty($handler['target_bundles'])) {
if ($bundle_entity_type_id = $target_entity_type->getBundleEntityType()) { if ($bundle_entity_type_id = $target_entity_type->getBundleEntityType()) {
...@@ -473,10 +475,19 @@ public static function onDependencyRemoval(FieldDefinitionInterface $field_defin ...@@ -473,10 +475,19 @@ public static function onDependencyRemoval(FieldDefinitionInterface $field_defin
foreach ($storage->loadMultiple($handler_settings['target_bundles']) as $bundle) { foreach ($storage->loadMultiple($handler_settings['target_bundles']) as $bundle) {
if (isset($dependencies[$bundle->getConfigDependencyKey()][$bundle->getConfigDependencyName()])) { if (isset($dependencies[$bundle->getConfigDependencyKey()][$bundle->getConfigDependencyName()])) {
unset($handler_settings['target_bundles'][$bundle->id()]); unset($handler_settings['target_bundles'][$bundle->id()]);
// If this bundle is also used in the 'auto_create_bundle'
// setting, disable the auto-creation feature completely.
$auto_create_bundle = !empty($handler_settings['auto_create_bundle']) ? $handler_settings['auto_create_bundle'] : FALSE;
if ($auto_create_bundle && $auto_create_bundle == $bundle->id()) {
$handler_settings['auto_create'] = NULL;
$handler_settings['auto_create_bundle'] = NULL;
}
$bundles_changed = TRUE; $bundles_changed = TRUE;
// In case we deleted the only target bundle allowed by the field // In case we deleted the only target bundle allowed by the field
// we have to log a warning message because the field will not // we have to log a critical message because the field will not
// function correctly anymore. // function correctly anymore.
if ($handler_settings['target_bundles'] === []) { if ($handler_settings['target_bundles'] === []) {
\Drupal::logger('entity_reference')->critical('The %target_bundle bundle (entity type: %target_entity_type) was deleted. As a result, the %field_name entity reference field (entity_type: %entity_type, bundle: %bundle) no longer has any valid bundle it can reference. The field is not working correctly anymore and has to be adjusted.', [ \Drupal::logger('entity_reference')->critical('The %target_bundle bundle (entity type: %target_entity_type) was deleted. As a result, the %field_name entity reference field (entity_type: %entity_type, bundle: %bundle) no longer has any valid bundle it can reference. The field is not working correctly anymore and has to be adjusted.', [
......
...@@ -106,9 +106,9 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen ...@@ -106,9 +106,9 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
'#placeholder' => $this->getSetting('placeholder'), '#placeholder' => $this->getSetting('placeholder'),
); );
if ($this->getSelectionHandlerSetting('auto_create')) { if ($this->getSelectionHandlerSetting('auto_create') && ($bundle = $this->getAutocreateBundle())) {
$element['#autocreate'] = array( $element['#autocreate'] = array(
'bundle' => $this->getAutocreateBundle(), 'bundle' => $bundle,
'uid' => ($entity instanceof EntityOwnerInterface) ? $entity->getOwnerId() : \Drupal::currentUser()->id() 'uid' => ($entity instanceof EntityOwnerInterface) ? $entity->getOwnerId() : \Drupal::currentUser()->id()
); );
} }
...@@ -147,18 +147,20 @@ public function massageFormValues(array $values, array $form, FormStateInterface ...@@ -147,18 +147,20 @@ public function massageFormValues(array $values, array $form, FormStateInterface
*/ */
protected function getAutocreateBundle() { protected function getAutocreateBundle() {
$bundle = NULL; $bundle = NULL;
if ($this->getSelectionHandlerSetting('auto_create')) { if ($this->getSelectionHandlerSetting('auto_create') && $target_bundles = $this->getSelectionHandlerSetting('target_bundles')) {
// If the 'target_bundles' setting is restricted to a single choice, we // If there's only one target bundle, use it.
// can use that. if (count($target_bundles) == 1) {
if (($target_bundles = $this->getSelectionHandlerSetting('target_bundles')) && count($target_bundles) == 1) {
$bundle = reset($target_bundles); $bundle = reset($target_bundles);
} }
// Otherwise use the first bundle as a fallback. // Otherwise use the target bundle stored in selection handler settings.
else { elseif (!$bundle = $this->getSelectionHandlerSetting('auto_create_bundle')) {
// @todo Expose a proper UI for choosing the bundle for autocreated // If no bundle has been set as auto create target means that there is
// entities in https://www.drupal.org/node/2412569. // an inconsistency in entity reference field settings.
$bundles = entity_get_bundles($this->getFieldSetting('target_type')); trigger_error(sprintf(
$bundle = key($bundles); "The '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.",
$this->fieldDefinition->getLabel(),
$this->fieldDefinition->getName()
), E_USER_WARNING);
} }
} }
......
...@@ -68,3 +68,39 @@ function field_update_8002() { ...@@ -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 over 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 (is_array($handler_settings) && !empty($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);
}
}
}
...@@ -250,7 +250,7 @@ function field_entity_bundle_delete($entity_type_id, $bundle) { ...@@ -250,7 +250,7 @@ function field_entity_bundle_delete($entity_type_id, $bundle) {
$field_config->save(); $field_config->save();
// In case we deleted the only target bundle allowed by the field we // In case we deleted the only target bundle allowed by the field we
// have to log a warning message because the field will not function // have to log a critical message because the field will not function
// correctly anymore. // correctly anymore.
if ($handler_settings['target_bundles'] === []) { if ($handler_settings['target_bundles'] === []) {
\Drupal::logger('entity_reference')->critical('The %target_bundle bundle (entity type: %target_entity_type) was deleted. As a result, the %field_name entity reference field (entity_type: %entity_type, bundle: %bundle) no longer has any valid bundle it can reference. The field is not working correctly anymore and has to be adjusted.', [ \Drupal::logger('entity_reference')->critical('The %target_bundle bundle (entity type: %target_entity_type) was deleted. As a result, the %field_name entity reference field (entity_type: %entity_type, bundle: %bundle) no longer has any valid bundle it can reference. The field is not working correctly anymore and has to be adjusted.', [
......
...@@ -7,6 +7,8 @@ ...@@ -7,6 +7,8 @@
namespace Drupal\field\Tests\EntityReference; namespace Drupal\field\Tests\EntityReference;
use Drupal\Component\Utility\Unicode;
use Drupal\field\Entity\FieldConfig;
use Drupal\Core\Entity\Entity; use Drupal\Core\Entity\Entity;
use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\field_ui\Tests\FieldUiTestTrait; use Drupal\field_ui\Tests\FieldUiTestTrait;
...@@ -315,7 +317,7 @@ public function testFieldAdminHandler() { ...@@ -315,7 +317,7 @@ public function testFieldAdminHandler() {
// Tests adding default values to autocomplete widgets. // Tests adding default values to autocomplete widgets.
Vocabulary::create(array('vid' => 'tags', 'name' => 'tags'))->save(); Vocabulary::create(array('vid' => 'tags', 'name' => 'tags'))->save();
$taxonomy_term_field_name = $this->createEntityReferenceField('taxonomy_term', 'tags'); $taxonomy_term_field_name = $this->createEntityReferenceField('taxonomy_term', ['tags']);
$field_path = 'node.' . $this->type . '.field_' . $taxonomy_term_field_name; $field_path = 'node.' . $this->type . '.field_' . $taxonomy_term_field_name;
$this->drupalGet($bundle_path . '/fields/' . $field_path . '/storage'); $this->drupalGet($bundle_path . '/fields/' . $field_path . '/storage');
$edit = [ $edit = [
...@@ -348,13 +350,13 @@ public function testAvailableFormatters() { ...@@ -348,13 +350,13 @@ public function testAvailableFormatters() {
Vocabulary::create(array('vid' => 'tags', 'name' => 'tags'))->save(); Vocabulary::create(array('vid' => 'tags', 'name' => 'tags'))->save();
// Create entity reference field with taxonomy term as a target. // Create entity reference field with taxonomy term as a target.
$taxonomy_term_field_name = $this->createEntityReferenceField('taxonomy_term', 'tags'); $taxonomy_term_field_name = $this->createEntityReferenceField('taxonomy_term', ['tags']);
// Create entity reference field with user as a target. // Create entity reference field with user as a target.
$user_field_name = $this->createEntityReferenceField('user'); $user_field_name = $this->createEntityReferenceField('user');
// Create entity reference field with node as a target. // Create entity reference field with node as a target.
$node_field_name = $this->createEntityReferenceField('node', $this->type); $node_field_name = $this->createEntityReferenceField('node', [$this->type]);
// Create entity reference field with date format as a target. // Create entity reference field with date format as a target.
$date_format_field_name = $this->createEntityReferenceField('date_format'); $date_format_field_name = $this->createEntityReferenceField('date_format');
...@@ -402,18 +404,79 @@ public function testAvailableFormatters() { ...@@ -402,18 +404,79 @@ public function testAvailableFormatters() {
)); ));
} }
/**
* Tests field settings for an entity reference field when the field has
* multiple target bundles and is set to auto-create the target entity.
*/
public function testMultipleTargetBundles() {
/** @var \Drupal\taxonomy\Entity\Vocabulary[] $vocabularies */
$vocabularies = [];
for ($i = 0; $i < 2; $i++) {
$vid = Unicode::strtolower($this->randomMachineName());
$vocabularies[$i] = Vocabulary::create([
'name' => $this->randomString(),
'vid' => $vid,
]);
$vocabularies[$i]->save();
}
// Create a new field pointing to the first vocabulary.
$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;
$this->drupalGet($path);
// Expect that there's no 'auto_create_bundle' selected.
$this->assertNoFieldByName('settings[handler_settings][auto_create_bundle]');
$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() . "']");
$edit = [
'settings[handler_settings][auto_create]' => TRUE,
'settings[handler_settings][auto_create_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->getSetting('handler_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());
// 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']));
}
/** /**
* Creates a new Entity Reference fields with a given target type. * Creates a new Entity Reference fields with a given target type.
* *
* @param $target_type * @param string $target_type
* The name of the target type * The name of the target type
* @param $bundle * @param string[] $bundles
* Name of the bundle * A list of bundle IDs. Defaults to [].
* Default = NULL *
* @return string * @return string
* Returns the generated field name * Returns the generated field name
*/ */
public function createEntityReferenceField($target_type, $bundle = NULL) { protected function createEntityReferenceField($target_type, $bundles = []) {
// Generates a bundle path for the newly created content type. // Generates a bundle path for the newly created content type.
$bundle_path = 'admin/structure/types/manage/' . $this->type; $bundle_path = 'admin/structure/types/manage/' . $this->type;
...@@ -422,7 +485,7 @@ public function createEntityReferenceField($target_type, $bundle = NULL) { ...@@ -422,7 +485,7 @@ public function createEntityReferenceField($target_type, $bundle = NULL) {
$storage_edit = $field_edit = array(); $storage_edit = $field_edit = array();
$storage_edit['settings[target_type]'] = $target_type; $storage_edit['settings[target_type]'] = $target_type;
if ($bundle) { foreach ($bundles as $bundle) {
$field_edit['settings[handler_settings][target_bundles][' . $bundle . ']'] = TRUE; $field_edit['settings[handler_settings][target_bundles][' . $bundle . ']'] = TRUE;
} }
...@@ -432,7 +495,6 @@ public function createEntityReferenceField($target_type, $bundle = NULL) { ...@@ -432,7 +495,6 @@ public function createEntityReferenceField($target_type, $bundle = NULL) {
return $field_name; return $field_name;
} }
/** /**
* Checks if a select element contains the specified options. * Checks if a select element contains the specified options.
* *
......
...@@ -7,9 +7,11 @@ ...@@ -7,9 +7,11 @@
namespace Drupal\field\Tests\EntityReference; namespace Drupal\field\Tests\EntityReference;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\field\Entity\FieldConfig; use Drupal\field\Entity\FieldConfig;
use Drupal\simpletest\WebTestBase; use Drupal\simpletest\WebTestBase;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\node\Entity\Node; use Drupal\node\Entity\Node;
use Drupal\field\Entity\FieldStorageConfig; use Drupal\field\Entity\FieldStorageConfig;
...@@ -20,7 +22,9 @@ ...@@ -20,7 +22,9 @@
*/ */
class EntityReferenceAutoCreateTest extends WebTestBase { class EntityReferenceAutoCreateTest extends WebTestBase {
public static $modules = ['node']; use EntityReferenceTestTrait;
public static $modules = ['node', 'taxonomy'];
/** /**
* The name of a content type that will reference $referencedType. * The name of a content type that will reference $referencedType.
...@@ -84,6 +88,9 @@ protected function setUp() { ...@@ -84,6 +88,9 @@ protected function setUp() {
'type' => 'entity_reference_autocomplete', 'type' => 'entity_reference_autocomplete',
)) ))
->save(); ->save();
$account = $this->drupalCreateUser(['access content', "create $this->referencingType content"]);
$this->drupalLogin($account);
} }
/** /**
...@@ -91,9 +98,6 @@ protected function setUp() { ...@@ -91,9 +98,6 @@ protected function setUp() {
* entity. * entity.
*/ */
public function testAutoCreate() { public function testAutoCreate() {
$user1 = $this->drupalCreateUser(array('access content', "create $this->referencingType content"));
$this->drupalLogin($user1);
$this->drupalGet('node/add/' . $this->referencingType); $this->drupalGet('node/add/' . $this->referencingType);
$this->assertFieldByXPath('//input[@id="edit-test-field-0-target-id" and contains(@class, "form-autocomplete")]', NULL, 'The autocomplete input element appears.'); $this->assertFieldByXPath('//input[@id="edit-test-field-0-target-id" and contains(@class, "form-autocomplete")]', NULL, 'The autocomplete input element appears.');
...@@ -136,4 +140,94 @@ public function testAutoCreate() { ...@@ -136,4 +140,94 @@ public function testAutoCreate() {
$this->assertText($referencing_node->label(), 'Referencing node label found.'); $this->assertText($referencing_node->label(), 'Referencing node label found.');
$this->assertText($referenced_node->label(), 'Referenced node label found.'); $this->assertText($referenced_node->label(), 'Referenced node label found.');
} }
/**
* Tests if an entity reference field having multiple target bundles is
* storing the auto-created entity in the right destination.
*/
public function testMultipleTargetBundles() {
/** @var \Drupal\taxonomy\Entity\Vocabulary[] $vocabularies */
$vocabularies = [];
for ($i = 0; $i < 2; $i++) {
$vid = Unicode::strtolower($this->randomMachineName());
$vocabularies[$i] = Vocabulary::create([
'name' => $this->randomMachineName(),
'vid' => $vid,
]);
$vocabularies[$i]->save();
}
// Create a taxonomy term entity reference field that saves the auto-created
// taxonomy terms in the second vocabulary from the two that were configured
// as targets.
$field_name = Unicode::strtolower($this->randomMachineName());
$handler_settings = [
'target_bundles' => [
$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'])
->save();
$term_name = $this->randomString();
$edit = [
$field_name . '[0][target_id]' => $term_name,
'title[0][value]' => $this->randomString(),
];
$this->drupalPostForm('node/add/' . $this->referencingType, $edit, 'Save');
/** @var \Drupal\taxonomy\Entity\Term $term */
$term = taxonomy_term_load_multiple_by_name($term_name);
$term = reset($term);
// The new term is expected to be stored in the second vocabulary.
$this->assertEqual($vocabularies[1]->id(), $term->bundle());
/** @var \Drupal\field\Entity\FieldConfig $field_config */
$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.
$handler_settings['auto_create_bundle'] = $vocabularies[0]->id();
$field_config->setSetting('handler_settings', $handler_settings);
$field_config->save();
$term_name = $this->randomString();
$edit = [
$field_name . '[0][target_id]' => $term_name,
'title[0][value]' => $this->randomString(),
];
$this->drupalPostForm('node/add/' . $this->referencingType, $edit, 'Save');
/** @var \Drupal\taxonomy\Entity\Term $term */
$term = taxonomy_term_load_multiple_by_name($term_name);
$term = reset($term);
// The second term is expected to be stored in the first vocabulary.
$this->assertEqual($vocabularies[0]->id(), $term->bundle());
// @todo Re-enable this test when WebTestBase::curlHeaderCallback() provides
// a way to catch and assert user-triggered errors.
// 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();
//
//$this->drupalGet('node/add/' . $this->referencingType);
//$error_message = 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.",
// $field_config->getLabel(),
// $field_config->getName()
//);
//$this->assertErrorLogged($error_message);
}
} }
...@@ -111,6 +111,26 @@ public function testFieldUpdate8002() { ...@@ -111,6 +111,26 @@ public function testFieldUpdate8002() {
$this->assertEqual(array_keys($referencable['article']), [$node_1->id()]); $this->assertEqual(array_keys($referencable['article']), [$node_1->id()]);
} }
/**
* Tests field_update_8003().