Skip to content
This repository has been archived by the owner on Nov 18, 2022. It is now read-only.

Commit

Permalink
Issue #3175759 by chr.fritsch, daniel.bosen: Support autocreation for…
Browse files Browse the repository at this point in the history
… views selection
  • Loading branch information
chrfritsch authored Oct 8, 2020
1 parent 4fcb8eb commit 73def20
Showing 1 changed file with 21 additions and 19 deletions.
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

0 comments on commit 73def20

Please sign in to comment.