Problem/Motivation
Drupal\dropzonejs\Controller\UploadController still wires its dependencies by hand through create() and a matching __construct(). Since Drupal 10.3, controllers can pull their services straight from type-hinted constructor arguments via Drupal\Core\DependencyInjection\AutowireTrait, which removes the boilerplate create() method entirely (change record #3395716).
There is one wrinkle: the controller currently injects the request object directly:
public static function create(ContainerInterface $container) {
return new static(
$container->get('dropzonejs.upload_handler'),
$container->get('request_stack')->getCurrentRequest()
);
}
AutowireTrait can only autowire actual services, and Symfony\Component\HttpFoundation\Request is not one — it is a request-scoped object produced by the request_stack service. Injecting the current Request into a service/controller is also discouraged, because the instance is captured at construction time and can go stale on sub-requests. So this cleanup should switch to injecting the request_stack service and calling getCurrentRequest() at the point of use.
Proposed resolution
- Add use Drupal\Core\DependencyInjection\AutowireTrait; and use AutowireTrait; in UploadController, and remove the manual create() method.
- Replace the injected Request with the request_stack service (Symfony\Component\HttpFoundation\RequestStack), type-hinted on the constructor so it autowires, and update handleUploads() to read the file from $this->requestStack->getCurrentRequest()->files->get('file').
- Use constructor property promotion, matching the style adopted in the OOP hook classes from #3576977.
Issue fork dropzonejs-3613233
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #3
velmir_taky commentedPushed a fix. The first pass broke the actual upload route —
AutowireTrait::create()couldn't resolveUploadHandlerInterfacebecause the service was only registered by id, so the controller 500'd and the functional upload tests errored.Added the autowiring alias in
dropzonejs.services.yml, and reworked the kernel test to build the controller through the container (::create()) instead ofnew, so the DI wiring is actually covered now — it goes red without the alias and green with it.Comment #4
csakiistvanComment #5
csakiistvanEnvironment
Prerequisites
dropzoneJavaScript library is installed inweb/libraries/dropzone./dropzonejs-test-standaloneusing thedropzonejs_media_entitywidget, pointing at theimagemedia type (same shape asentity_browser.browser.dropzonejs_eb_standalone_test.ymlshipped intests/modules/dropzonejs_test).ddev drush uliSteps
/dropzonejs-test-standalone, upload an image and press Select entities. Confirm the upload succeeds and a media item is created.UploadControllerusesAutowireTraitwith promoted constructor properties instead of a manualcreate(), injectsRequestStackinstead of the currentRequest,dropzonejs.services.ymlgains an autowiring alias forUploadHandlerInterface, and the kernel test builds the controller through the container.ddev drush cr/dropzonejs-test-standalone: drop an image into the dropzone and press Select entities.Expected results
/dropzonejs/uploadkeeps working after the refactor — no 500 error, the thumbnail appears and the media item is created.create()method.DropzoneJsUploadControllerTestpasses and now exercises the real DI wiring.Actual results
Before the fix, uploads through the standalone entity browser worked as expected with the hand-wired
create(). After applying MR !36, the behaviour is unchanged in the browser: the file uploads, the preview renders and Select entities creates the media item, with no error on/dropzonejs/upload. Building the controller through the container resolves cleanly, which confirms the autowiring alias forDrupal\dropzonejs\UploadHandlerInterfaceadded indropzonejs.services.ymlfixes the 500 reported against the first pass of this MR. The kernel test passes (2 tests, 15 assertions); the reported deprecations come from core'sFileSystem::basename()and are unrelated to this change.Testing produced with the assistance of an LLM.