From 945ac1a98afd53eab3cf7ec0e9c2c94bad6feaf5 Mon Sep 17 00:00:00 2001 From: Christian Fritsch Date: Thu, 8 Oct 2020 14:31:56 +0200 Subject: [PATCH] fix: autocreate for views selection --- .../Select2EntityReferenceWidget.php | 40 ++++++++++--------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/src/Plugin/Field/FieldWidget/Select2EntityReferenceWidget.php b/src/Plugin/Field/FieldWidget/Select2EntityReferenceWidget.php index b3f798e5..bea14c3c 100644 --- a/src/Plugin/Field/FieldWidget/Select2EntityReferenceWidget.php +++ b/src/Plugin/Field/FieldWidget/Select2EntityReferenceWidget.php @@ -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); } }