Error when saving form with user reference field and auto-create enabled due to missing 'name' field

Created on 8 August 2024, 3 months ago
Updated 19 September 2024, about 2 months ago

Problem/Motivation

When adding a reference field of type "User" to a content type (e.g., articles) with the following configurations:

  • Allowed number of values set to "unlimited"
  • "Create referenced entities if they don't already exist" enabled
  • Widget set to "Autocomplete (Tags style)" in Manage form display

An error occurs when a new user name (one that does not exist) is entered and the form is saved. The error message displayed is:

<pre>
The website encountered an unexpected error. Try again later.

Drupal\Core\Entity\EntityStorageException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'name' cannot be null: INSERT INTO "users_field_data" ("uid", "langcode", "preferred_langcode", "preferred_admin_langcode", "name", "pass", "mail", "timezone", "status", "created", "changed", "access", "login", "init", "default_langcode") VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6, :db_insert_placeholder_7, :db_insert_placeholder_8, :db_insert_placeholder_9, :db_insert_placeholder_10, :db_insert_placeholder_11, :db_insert_placeholder_12, :db_insert_placeholder_13, :db_insert_placeholder_14); Array ( [:db_insert_placeholder_0] => 4 [:db_insert_placeholder_1] => en [:db_insert_placeholder_2] => en [:db_insert_placeholder_3] => [:db_insert_placeholder_4] => [:db_insert_placeholder_5] => [:db_insert_placeholder_6] => [:db_insert_placeholder_7] => Asia/Tehran [:db_insert_placeholder_8] => 0 [:db_insert_placeholder_9] => 1723121108 [:db_insert_placeholder_10] => 1723121108 [:db_insert_placeholder_11] => 0 [:db_insert_placeholder_12] => 0 [:db_insert_placeholder_13] => [:db_insert_placeholder_14] => 1 ) in Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (line 816 of C:\Server\htdocs\drupal-11.0.0\core\lib\Drupal\Core\Entity\Sql\SqlContentEntityStorage.php).

#0 drupal-11.0.0\core\lib\Drupal\Core\Entity\EntityBase.php(354): Drupal\Core\Entity\Sql\SqlContentEntityStorage->save(Object(Drupal\node\Entity\Node))
#1 drupal-11.0.0\core\modules\node\src\NodeForm.php(277): Drupal\Core\Entity\EntityBase->save()
#2 [internal function]: Drupal\node\NodeForm->save(Array, Object(Drupal\Core\Form\FormState))
#3 drupal-11.0.0\core\lib\Drupal\Core\Form\FormSubmitter.php(105): call_user_func_array(Array, Array)
#4 drupal-11.0.0\core\lib\Drupal\Core\Form\FormSubmitter.php(43): Drupal\Core\Form\FormSubmitter->executeSubmitHandlers(Array, Object(Drupal\Core\Form\FormState))
#5 drupal-11.0.0\core\lib\Drupal\Core\Form\FormBuilder.php(589): Drupal\Core\Form\FormSubmitter->doSubmitForm(Array, Object(Drupal\Core\Form\FormState))
#6 drupal-11.0.0\core\lib\Drupal\Core\Form\FormBuilder.php(321): Drupal\Core\Form\FormBuilder->processForm('node_test_form', Array, Object(Drupal\Core\Form\FormState))
#7 drupal-11.0.0\core\lib\Drupal\Core\Controller\FormController.php(73): Drupal\Core\Form\FormBuilder->buildForm(Object(Drupal\node\NodeForm), Object(Drupal\Core\Form\FormState))
#8 [internal function]: Drupal\Core\Controller\FormController->getContentResult(Object(Symfony\Component\HttpFoundation\Request), Object(Drupal\Core\Routing\RouteMatch))
#9 drupal-11.0.0\core\lib\Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber.php(123): call_user_func_array(Array, Array)
#10 drupal-11.0.0\core\lib\Drupal\Core\Render\Renderer.php(593): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
#11 drupal-11.0.0\core\lib\Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber.php(121): Drupal\Core\Render\Renderer->executeInRenderContext(Object(Drupal\Core\Render\RenderContext), Object(Closure))
#12 drupal-11.0.0\core\lib\Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber.php(97): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array)
#13 drupal-11.0.0\vendor\symfony\http-kernel\HttpKernel.php(183): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
#14 drupal-11.0.0\vendor\symfony\http-kernel\HttpKernel.php(76): Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object(Symfony\Component\HttpFoundation\Request), 1)
#15 drupal-11.0.0\core\lib\Drupal\Core\StackMiddleware\Session.php(53): Symfony\Component\HttpKernel\HttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#16 drupal-11.0.0\core\lib\Drupal\Core\StackMiddleware\KernelPreHandle.php(48): Drupal\Core\StackMiddleware\Session->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#17 drupal-11.0.0\core\lib\Drupal\Core\StackMiddleware\ContentLength.php(28): Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#18 drupal-11.0.0\core\modules\big_pipe\src\StackMiddleware\ContentLength.php(32): Drupal\Core\StackMiddleware\ContentLength->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#19 drupal-11.0.0\core\modules\page_cache\src\StackMiddleware\PageCache.php(106): Drupal\big_pipe\StackMiddleware\ContentLength->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#20 drupal-11.0.0\core\modules\page_cache\src\StackMiddleware\PageCache.php(85): Drupal\page_cache\StackMiddleware\PageCache->pass(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#21 drupal-11.0.0\core\lib\Drupal\Core\StackMiddleware\ReverseProxyMiddleware.php(48): Drupal\page_cache\StackMiddleware\PageCache->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#22 drupal-11.0.0\core\lib\Drupal\Core\StackMiddleware\NegotiationMiddleware.php(51): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#23 drupal-11.0.0\core\lib\Drupal\Core\StackMiddleware\AjaxPageState.php(36): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#24 drupal-11.0.0\core\lib\Drupal\Core\StackMiddleware\StackedHttpKernel.php(51): Drupal\Core\StackMiddleware\AjaxPageState->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#25 drupal-11.0.0\core\lib\Drupal\Core\DrupalKernel.php(709): Drupal\Core\StackMiddleware\StackedHttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true)
#26 drupal-11.0.0\index.php(19): Drupal\Core\DrupalKernel->handle(Object(Symfony\Component\HttpFoundation\Request))
#27 {main}
</pre>

It appears that in the Entity Autocomplete widget, values are stored as $matches[] = ['value' => $key, 'label' => $label];. When a new user is created, the username is stored as the label and the name key is missing.

Steps to reproduce

  1. Add a reference field of type "User" to a content type (e.g., articles).
  2. Set the allowed number of values to "unlimited".
  3. Enable "Create referenced entities if they don't already exist".
  4. Set the widget to "Autocomplete (Tags style)" in the Manage form display.
  5. Create or edit a node of this content type.
  6. Enter a new user name that does not exist.
  7. Save the form.

Proposed resolution

Investigate and ensure that the name field is properly populated when creating a new referenced user entity through the autocomplete widget. Possible solutions include:

  • Validate that the name field is not null before attempting to save the user entity.
  • Ensure the autocomplete widget correctly assigns the user-entered value to the name field of the user entity.

Remaining tasks

  • Identify the root cause of the name field being null during the save process.
  • Implement a fix to ensure the name field is populated.
  • Write automated tests to verify that creating new users through this widget works correctly and does not cause database errors.

User interface changes

No major user interface changes are expected. The fix will ensure that the existing functionality works as intended without errors.

Introduced terminology

No new terminology introduced.

API changes

No API changes are anticipated. The fix will address the internal handling of user entity creation.

Data model changes

No data model changes are expected.

Release notes snippet

Fixed an issue where creating new referenced user entities through the "Autocomplete (Tags style)" widget could result in a database error due to a missing name field.

🐛 Bug report
Status

Active

Version

11.0 🔥

Component
Field UI 

Last updated 11 days ago

Created by

🇵🇱Poland mahyarss

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024