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 26b2526..667d013 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/EntityReferenceAutocompleteWidget.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/EntityReferenceAutocompleteWidget.php @@ -106,9 +106,9 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen '#placeholder' => $this->getSetting('placeholder'), ); - if ($this->getSelectionHandlerSetting('auto_create')) { + if ($this->getSelectionHandlerSetting('auto_create') && ($bundle = $this->getAutocreateBundle())) { $element['#autocreate'] = array( - 'bundle' => $this->getAutocreateBundle(), + 'bundle' => $bundle, 'uid' => ($entity instanceof EntityOwnerInterface) ? $entity->getOwnerId() : \Drupal::currentUser()->id() ); } @@ -144,10 +144,6 @@ public function massageFormValues(array $values, array $form, FormStateInterface * * @return string * The bundle name. - * - * @throws \InvalidArgumentException - * Thrown when the field allows references from multiple target bundles, - * 'auto_create' is enabled and 'auto_create_bundle' is not set. */ protected function getAutocreateBundle() { $bundle = NULL; @@ -160,11 +156,11 @@ protected function getAutocreateBundle() { 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. - throw new \InvalidArgumentException(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.", + 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); } } diff --git a/core/modules/field/src/Tests/EntityReference/EntityReferenceAutoCreateTest.php b/core/modules/field/src/Tests/EntityReference/EntityReferenceAutoCreateTest.php index efeb846..acaabdb 100644 --- a/core/modules/field/src/Tests/EntityReference/EntityReferenceAutoCreateTest.php +++ b/core/modules/field/src/Tests/EntityReference/EntityReferenceAutoCreateTest.php @@ -213,30 +213,21 @@ public function testMultipleTargetBundles() { // The second term is expected to be stored in the first vocabulary. $this->assertEqual($vocabularies[0]->id(), $term->bundle()); - // 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(); + // @todo Re-enable this test when WebTestBase::curlHeaderCallback() provides + // a way to catch and assert user-triggered errors. - // If we use $this->drupalGet() we cannot catch the exception because the - // form builder runs in other PHP space. Instead we are only building the - // form in order to check if the exception is thrown. - try { - /** @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."); - } - catch (\InvalidArgumentException $e) { - $this->assertIdentical( - $e->getMessage(), - 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() - ) - ); - } + // 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); } }