Skip to content
This repository was archived by the owner on Nov 18, 2022. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 21 additions & 19 deletions src/Plugin/Field/FieldWidget/Select2EntityReferenceWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,27 +253,29 @@ protected static function prepareFieldValues(array $values, array $element) {
protected function getAutocreateBundle() {
$bundle = NULL;
if ($this->getSelectionHandlerSetting('auto_create')) {
if ($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')) {
// If no bundle has been set as auto create target means that there is
// an inconsistency in entity reference field settings.
trigger_error(sprintf(
"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);
}
// If a bundle is explicitly defined, use it.
if ($bundle = $this->getSelectionHandlerSetting('auto_create_bundle')) {
return $bundle;
}

$target_bundles = $this->getSelectionHandlerSetting('target_bundles');
// If there's no target bundle at all, use the target_type. It's the
// default for bundleless entity types.
if (empty($target_bundles)) {
$bundle = $this->getFieldSetting('target_type');
}
// If there's only one target bundle, use it.
elseif (count($target_bundles) == 1) {
$bundle = reset($target_bundles);
}
else {
$entity_definition = $this->entityTypeManager->getDefinition($this->getFieldSetting('target_type'));
if (!$entity_definition->getBundleEntityType()) {
$bundle = $this->getFieldSetting('target_type');
}
// If no bundle has been set as auto create target means that there is
// an inconsistency in entity reference field settings.
trigger_error(sprintf(
"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);
}
}

Expand Down