Warning: Creating default object from empty value in DrupalCoreEntityContentEntityForm->__construct()

I’m currently working on a site that has a number of custom entities. Two of these entities (Primary and Secondary) are referenced using entity reference where the cardinality of the entity reference for Secondary is set to unlimited. So when creating a Primary entity, the user can create multiple referenced Secondary entities.

I’ve set the field widget for field_secondary (the entity reference field) to use Inline Entity Form. When creating the Primary entity, I’m able to create a single Secondary entity and save this. It creates the appropriate Primary and Secondary entities. However, when clicking on the “Add another item” to add a second Secondary entity, I get a javascript/ajax error and in my dblog, I get the following entries:

Warning: Creating default object from empty value in DrupalCoreEntityContentEntityForm->__construct() (line 59 of /var/www/drupalvm/web/core/lib/Drupal/Core/Entity/ContentEntityForm.php) #0 /var/www/drupalvm/web/core/includes/bootstrap.inc(552): _drupal_error_handler_real(2, 'Creating defaul...', '/var/www/drupal...', 59, Array) #1 /var/www/drupalvm/web/core/lib/Drupal/Core/Entity/ContentEntityForm.php(59): _drupal_error_handler(2, 'Creating defaul...', '/var/www/drupal...', 59, Array) #2 /var/www/drupalvm/web/core/lib/Drupal/Core/Entity/ContentEntityForm.php(70): DrupalCoreEntityContentEntityForm->__construct(Object(DrupalCoreEntityEntityManager), Object(DrupalCoreEntityEntityTypeBundleInfo), Object(DrupalComponentDatetimeTime)) #3 /var/www/drupalvm/web/core/lib/Drupal/Core/DependencyInjection/ClassResolver.php(28): DrupalCoreEntityContentEntityForm::create(Object(DrupalCoreDependencyInjectionContainer)) #4 /var/www/drupalvm/web/core/lib/Drupal/Core/Entity/EntityTypeManager.php(187): DrupalCoreDependencyInjectionClassResolver->getInstanceFromDefinition('Drupalnuffield...') #5 /var/www/drupalvm/web/core/lib/Drupal/Core/Entity/EntityManager.php(82): DrupalCoreEntityEntityTypeManager->getFormObject('primary', 'default') #6 /var/www/drupalvm/web/core/lib/Drupal/Core/Entity/EntityFormBuilder.php(44): DrupalCoreEntityEntityManager->getFormObject('primary', 'default') #7 /var/www/drupalvm/web/modules/custom/application/src/Controller/ApplicationController.php(52): DrupalCoreEntityEntityFormBuilder->getForm(Object(DrupalapplicationEntityApplication), 'default') #8 [internal function]: DrupalapplicationControllerApplicationController->form_handler(Object(DrupalapplicationEntityApplication), 'default') #9 /var/www/drupalvm/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(123): call_user_func_array(Array, Array) #10 /var/www/drupalvm/web/core/lib/Drupal/Core/Render/Renderer.php(574): DrupalCoreEventSubscriberEarlyRenderingControllerWrapperSubscriber->DrupalCoreEventSubscriber{closure}() #11 /var/www/drupalvm/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(124): DrupalCoreRenderRenderer->executeInRenderContext(Object(DrupalCoreRenderRenderContext), Object(Closure)) #12 /var/www/drupalvm/web/core/lib/Drupal/Core/EventSubscriber/EarlyRenderingControllerWrapperSubscriber.php(97): DrupalCoreEventSubscriberEarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) #13 [internal function]: DrupalCoreEventSubscriberEarlyRenderingControllerWrapperSubscriber->DrupalCoreEventSubscriber{closure}() #14 /var/www/drupalvm/vendor/symfony/http-kernel/HttpKernel.php(144): call_user_func_array(Object(Closure), Array) #15 /var/www/drupalvm/vendor/symfony/http-kernel/HttpKernel.php(64): SymfonyComponentHttpKernelHttpKernel->handleRaw(Object(SymfonyComponentHttpFoundationRequest), 1) #16 /var/www/drupalvm/web/core/lib/Drupal/Core/StackMiddleware/Session.php(57): SymfonyComponentHttpKernelHttpKernel->handle(Object(SymfonyComponentHttpFoundationRequest), 1, true) #17 /var/www/drupalvm/web/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(47): DrupalCoreStackMiddlewareSession->handle(Object(SymfonyComponentHttpFoundationRequest), 1, true) #18 /var/www/drupalvm/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(99): DrupalCoreStackMiddlewareKernelPreHandle->handle(Object(SymfonyComponentHttpFoundationRequest), 1, true) #19 /var/www/drupalvm/web/core/modules/page_cache/src/StackMiddleware/PageCache.php(78): Drupalpage_cacheStackMiddlewarePageCache->pass(Object(SymfonyComponentHttpFoundationRequest), 1, true) #20 /var/www/drupalvm/web/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(47): Drupalpage_cacheStackMiddlewarePageCache->handle(Object(SymfonyComponentHttpFoundationRequest), 1, true) #21 /var/www/drupalvm/web/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(50): DrupalCoreStackMiddlewareReverseProxyMiddleware->handle(Object(SymfonyComponentHttpFoundationRequest), 1, true) #22 /var/www/drupalvm/vendor/stack/builder/src/Stack/StackedHttpKernel.php(23): DrupalCoreStackMiddlewareNegotiationMiddleware->handle(Object(SymfonyComponentHttpFoundationRequest), 1, true) #23 /var/www/drupalvm/web/core/lib/Drupal/Core/DrupalKernel.php(656): StackStackedHttpKernel->handle(Object(SymfonyComponentHttpFoundationRequest), 1, true) #24 /var/www/drupalvm/web/index.php(19): DrupalCoreDrupalKernel->handle(Object(SymfonyComponentHttpFoundationRequest)) #25 {main}.

A second error is also showing in dblog:

LogicException: The database connection is not serializable. This probably means you are serializing an object that has an indirect reference to the database connection. Adjust your code so that is not necessary. Alternatively, look at DependencySerializationTrait as a temporary solution. in DrupalCoreDatabaseConnection->__sleep() (line 1471 of /var/www/drupalvm/web/core/lib/Drupal/Core/Database/Connection.php).

I’ve tried changing the field form widget to Autcomplete and I’m getting the same problem. Happy to share code.

Cheers

Drupal version: 


Source: https://www.drupal.org/taxonomy/term/4/feed