Managed File Element Breaks Dependency Injection on Form Class

Created on 5 June 2016, over 8 years ago
Updated 8 November 2024, 13 days ago

I'm trying to build a form that uses the database service and a managed_file element. It works fine when I don't submit a file, but when I upload a file, the connection object I have on my form class is NULL in submitForm().

I've simplified the form down to a demo module which I'm attaching. Enable the module and go to /managed-form-demo. If you submit the form without uploading a file, everything works fine, and the select statement in submitForm() works.

If you upload a file, the connection object will be NULL in submitForm(), which results in a fatal error on the select() call. The file part appears to be working: I can see the file in sites/default/files, and it's in the file_managed table, as well. But, it seems like there's some side effect that is breaking dependency injection aspect of this class.

Am I doing something wrong with how I'm injecting the database service or using the managed_file element? Or is this a bug?

Here is my environment:

PHP: 7.0.6
Drupal: 8.1.2
OS: Ubuntu 14.04.4
Webserver: Apache

Here is the full error message:

The website encountered an unexpected error. Please try again later.
Error: Call to a member function select() on null in Drupal\filedemo\Form\DemoForm->submitForm() (line 57 of modules/custom/filedemo/src/Form/DemoForm.php).
Drupal\filedemo\Form\DemoForm->submitForm(Array, Object)
call_user_func_array(Array, Array) (Line: 111)
Drupal\Core\Form\FormSubmitter->executeSubmitHandlers(Array, Object) (Line: 51)
Drupal\Core\Form\FormSubmitter->doSubmitForm(Array, Object) (Line: 583)
Drupal\Core\Form\FormBuilder->processForm('filedemo_demo_form', Array, Object) (Line: 314)
Drupal\Core\Form\FormBuilder->buildForm('filedemo_demo_form', Object) (Line: 74)
Drupal\Core\Controller\FormController->getContentResult(Object, Object)
call_user_func_array(Array, Array) (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 574)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 124)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}()
call_user_func_array(Object, Array) (Line: 139)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 62)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 98)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 77)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 50)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 628)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

Please let me know if there is anything else I should test or check. Thanks.

🐛 Bug report
Status

Closed: works as designed

Version

9.3

Component

forms system

Created by

🇺🇸United States zengenuity

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇮🇹Italy robertoperuzzo 🇮🇹 Tezze sul Brenta, VI

    In this case you may add the `Drupal\Core\DependencyInjection\DependencySerializationTrait` to serialize the injected object, and you will have it in the submitForm() method.

    class YourForm extends FormBase {
    
      use DependencySerializationTrait;
    
       // The rest of your code.
    
    
Production build 0.71.5 2024