Hi there! First I wanted to say thank you so much for the port. It is working great for text fields and a few others. I might have missed something but filing this issue for posterity. ^_^

Problem/Motivation

The Field Default Token module is not correctly working for Entity Reference Fields. When you save a token in a field of type entity reference and go back to the field edit screen you will see the token has not been saved. Other fields do not have this problem.

Proposed resolution

In hook_field_config_presave the following logic doesn't seem to work for entity reference fields and the $has_token never gets set to true.

  /** @var \Drupal\field\FieldConfigInterface|\Drupal\Core\Field\FieldConfigInterface $field_config */
  $has_tokens = FALSE;
  foreach ($field_config->getDefaultValueLiteral() as $item) {

The getDefaultValueLiteral doesn't work for entity reference items and returns empty.

Remaining tasks

N/A

User interface changes

N/A

API changes

N/A

Data model changes

N/A

Comments

sylus created an issue. See original summary.

Sturgelose’s picture

Using a bit the debugger, the problem is as @sylus says.
I've found out that the default value is not obtained via $field_config->getDefaultValueLiteral() but via $field_config['default_value_input']['default_value_token']

I think that adding another variable such as $isEntity would help a bit, or at least adding some more conditions to handle the case of EntityReference

cmarcera’s picture

Not sure if this is useful, but when adding [current-user:uid] to a User Entity Reference field, I get the following error and log messages:

Message Notice: Undefined index: [current-user:uid] in Drupal\Core\Field\EntityReferenceFieldItemList->defaultValuesFormSubmit() (line 126 of /var/www/drupal/web/core/lib/Drupal/Core/Field/EntityReferenceFieldItemList.php) #0 /var/www/drupal/web/core/includes/bootstrap.inc(587): _drupal_error_handler_real(8, 'Undefined index...', '/var/www/drupal...', 126, Array) #1 /var/www/drupal/web/core/lib/Drupal/Core/Field/EntityReferenceFieldItemList.php(126): _drupal_error_handler(8, 'Undefined index...', '/var/www/drupal...', 126, Array) #2 /var/www/drupal/web/core/modules/field_ui/src/Form/FieldConfigEditForm.php(197): Drupal\Core\Field\EntityReferenceFieldItemList->defaultValuesFormSubmit(Array, Array, Object(Drupal\Core\Form\FormState)) #3 [internal function]: Drupal\field_ui\Form\FieldConfigEditForm->submitForm(Array, Object(Drupal\Core\Form\FormState)) #4 /var/www/drupal/web/core/lib/Drupal/Core/Form/FormSubmitter.php(111): call_user_func_array(Array, Array) #5 /var/www/drupal/web/core/lib/Drupal/Core/Form/FormSubmitter.php(51): Drupal\Core\Form\FormSubmitter->executeSubmitHandlers(Array, Object(Drupal\Core\Form\FormState)) #6 /var/www/drupal/web/core/lib/Drupal/Core/Form/FormBuilder.php(590): Drupal\Core\Form\FormSubmitter->doSubmitForm(Array, Object(Drupal\Core\Form\FormState)) #7 /var/www/drupal/web/core/lib/Drupal/Core/Form/FormBuilder.php(319): Drupal\Core\Form\FormBuilder->processForm('field_config_ed...', Array, Object(Drupal\Core\Form\FormState)) #8 /var/www/drupal/web/core/lib/Drupal/Core/Controller/FormController.php(93): Drupal\Core\Form\FormBuilder->buildForm('field_config_ed...', Object(Drupal\Core\Form\FormState)) #9 [internal function]: Drupal\Core\Controller\FormController->getContentResult(Object(Symfony\Component\HttpFoundation\Request), Object(Drupal\Core\Routing\RouteMatch)) #10 /var/www/drupal/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(123): call_user_func_array(Array, Array) #11 /var/www/drupal/web/core/lib/Drupal/Core/Render/Renderer.php(582): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() #12 /var/www/drupal/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(124): Drupal\Core\Render\Renderer->executeInRenderContext(Object(Drupal\Core\Render\RenderContext), Object(Closure)) #13 /var/www/drupal/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(97): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) #14 /var/www/drupal/vendor/symfony/http-kernel/HttpKernel.php(151): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() #15 /var/www/drupal/vendor/symfony/http-kernel/HttpKernel.php(68): Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object(Symfony\Component\HttpFoundation\Request), 1) #16 /var/www/drupal/web/core/lib/Drupal/Core/StackMiddleware/Session.php(57): Symfony\Component\HttpKernel\HttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #17 /var/www/drupal/web/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(47): Drupal\Core\StackMiddleware\Session->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #18 /var/www/drupal/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(106): Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #19 /var/www/drupal/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(85): Drupal\page_cache\StackMiddleware\PageCache->pass(Object(Symfony\Component\HttpFoundation\Request), 1, true) #20 /var/www/drupal/web/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(47): Drupal\page_cache\StackMiddleware\PageCache->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #21 /var/www/drupal/web/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(52): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #22 /var/www/drupal/vendor/stack/builder/src/Stack/StackedHttpKernel.php(23): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #23 /var/www/drupal/web/core/lib/Drupal/Core/DrupalKernel.php(693): Stack\StackedHttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #24 /var/www/drupal/web/index.php(19): Drupal\Core\DrupalKernel->handle(Object(Symfony\Component\HttpFoundation\Request)) #25 {main}.

Error: Call to a member function uuid() on null in Drupal\Core\Field\EntityReferenceFieldItemList->defaultValuesFormSubmit() (line 126 of /var/www/drupal/web/core/lib/Drupal/Core/Field/EntityReferenceFieldItemList.php) #0 /var/www/drupal/web/core/modules/field_ui/src/Form/FieldConfigEditForm.php(197): Drupal\Core\Field\EntityReferenceFieldItemList->defaultValuesFormSubmit(Array, Array, Object(Drupal\Core\Form\FormState)) #1 [internal function]: Drupal\field_ui\Form\FieldConfigEditForm->submitForm(Array, Object(Drupal\Core\Form\FormState)) #2 /var/www/drupal/web/core/lib/Drupal/Core/Form/FormSubmitter.php(111): call_user_func_array(Array, Array) #3 /var/www/drupal/web/core/lib/Drupal/Core/Form/FormSubmitter.php(51): Drupal\Core\Form\FormSubmitter->executeSubmitHandlers(Array, Object(Drupal\Core\Form\FormState)) #4 /var/www/drupal/web/core/lib/Drupal/Core/Form/FormBuilder.php(590): Drupal\Core\Form\FormSubmitter->doSubmitForm(Array, Object(Drupal\Core\Form\FormState)) #5 /var/www/drupal/web/core/lib/Drupal/Core/Form/FormBuilder.php(319): Drupal\Core\Form\FormBuilder->processForm('field_config_ed...', Array, Object(Drupal\Core\Form\FormState)) #6 /var/www/drupal/web/core/lib/Drupal/Core/Controller/FormController.php(93): Drupal\Core\Form\FormBuilder->buildForm('field_config_ed...', Object(Drupal\Core\Form\FormState)) #7 [internal function]: Drupal\Core\Controller\FormController->getContentResult(Object(Symfony\Component\HttpFoundation\Request), Object(Drupal\Core\Routing\RouteMatch)) #8 /var/www/drupal/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(123): call_user_func_array(Array, Array) #9 /var/www/drupal/web/core/lib/Drupal/Core/Render/Renderer.php(582): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() #10 /var/www/drupal/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(124): Drupal\Core\Render\Renderer->executeInRenderContext(Object(Drupal\Core\Render\RenderContext), Object(Closure)) #11 /var/www/drupal/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(97): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) #12 /var/www/drupal/vendor/symfony/http-kernel/HttpKernel.php(151): Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() #13 /var/www/drupal/vendor/symfony/http-kernel/HttpKernel.php(68): Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object(Symfony\Component\HttpFoundation\Request), 1) #14 /var/www/drupal/web/core/lib/Drupal/Core/StackMiddleware/Session.php(57): Symfony\Component\HttpKernel\HttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #15 /var/www/drupal/web/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(47): Drupal\Core\StackMiddleware\Session->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #16 /var/www/drupal/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(106): Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #17 /var/www/drupal/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(85): Drupal\page_cache\StackMiddleware\PageCache->pass(Object(Symfony\Component\HttpFoundation\Request), 1, true) #18 /var/www/drupal/web/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(47): Drupal\page_cache\StackMiddleware\PageCache->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #19 /var/www/drupal/web/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(52): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #20 /var/www/drupal/vendor/stack/builder/src/Stack/StackedHttpKernel.php(23): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #21 /var/www/drupal/web/core/lib/Drupal/Core/DrupalKernel.php(693): Stack\StackedHttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #22 /var/www/drupal/web/index.php(19): Drupal\Core\DrupalKernel->handle(Object(Symfony\Component\HttpFoundation\Request)) #23 {main}.

Christopher Riley’s picture

Any word on getting this issue resolved?

delty’s picture

Any update on this? Would be great if this finally worked on entity reference fields.

brenk28’s picture

Here's an attempted patch.

marassa’s picture

@brenk28, the patch did not work for me (core 9.5.11) - the default value still not saved. Eventually I bit the bullet and switched to Entity Prepopulate module which does basically the same and does not have this (and other) problems.