Problem/Motivation

We duplicate the same logic of getting uploaded files from the request in four different locations:

  1. file_save_upload()
  2. file_managed_file_save_upload()
  3. \Drupal\config\Form\ConfigImportForm::validateForm()
  4. (\Drupal\Core\Render\Element\File::valueCallback())

Steps to reproduce

N/A

Proposed resolution

Create a UploadedFilesExtractor::extractUploadedFiles() and remove the duplicate code.

After review, the fourth usage in \Drupal\Core\Render\Element\File::valueCallback() is different and strange and is not converted here, as it returns the object cast to an array. That might need to be changed to something more useful, but that's not in scope of this issue.

Remaining tasks

Check the last sentence of the change record and the use of $form_field_name

User interface changes

N/A

Introduced terminology

N/A

API changes

Added a new service UploadedFilesExtractor::extractUploadedFiles()

Data model changes

N/A

Release notes snippet

Issue fork drupal-3555115

Command icon 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

kim.pepper created an issue. See original summary.

kim.pepper’s picture

Status: Active » Needs review
smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative

Would this count as an API change? If it should get an MR.

Moving to NW as a number of functional tests seemed to fail.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

nicxvan made their first commit to this issue’s fork.

nicxvan’s picture

Rebasing on main so we can see what the actual failures are.

I think this should be a service, passing in the current request seems wrong since every use requires that.

nicxvan’s picture

Ok I converted it to a service and identified the error:

Exception: Serialization of 'Symfony\Component\HttpFoundation\File\UploadedFile' is not allowed in serialize() (line 119 of core/lib/Drupal/Core/Batch/BatchStorage.php).

Drupal\Core\Batch\BatchStorage->create() (Line: 107)
Drupal\Core\ProxyClass\Batch\BatchStorage->create() (Line: 807)
batch_process() (Line: 56)
Drupal\Core\Form\FormSubmitter->doSubmitForm() (Line: 624)
Drupal\Core\Form\FormBuilder->processForm() (Line: 347)
Drupal\Core\Form\FormBuilder->buildForm() (Line: 73)
Drupal\Core\Controller\FormController->getContentResult()
call_user_func_array() (Line: 123)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->{closure:Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber::wrapControllerExecutionInRenderContext():121}() (Line: 638)
Drupal\Core\Render\Renderer::{closure:Drupal\Core\Render\Renderer::executeInRenderContext():638}()
Fiber->resume() (Line: 653)
Drupal\Core\Render\Renderer->executeInRenderContext() (Line: 121)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext() (Line: 97)
Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->{closure:Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber::onController():96}() (Line: 183)
Symfony\Component\HttpKernel\HttpKernel->handleRaw() (Line: 76)
Symfony\Component\HttpKernel\HttpKernel->handle() (Line: 53)
Drupal\Core\StackMiddleware\Session->handle() (Line: 48)
Drupal\Core\StackMiddleware\KernelPreHandle->handle() (Line: 28)
Drupal\Core\StackMiddleware\ContentLength->handle() (Line: 32)
Drupal\big_pipe\StackMiddleware\ContentLength->handle() (Line: 118)
Drupal\page_cache\StackMiddleware\PageCache->pass() (Line: 92)
Drupal\page_cache\StackMiddleware\PageCache->handle() (Line: 48)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle() (Line: 51)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle() (Line: 61)
Drupal\Core\StackMiddleware\AjaxPageState->handle() (Line: 54)
Drupal\Core\StackMiddleware\StackedHttpKernel->handle() (Line: 745)
Drupal\Core\DrupalKernel->handle() (Line: 19)
kim.pepper’s picture

Thanks for picking this up. I'm not sure why UploadedFile is now getting serialized with the form data.

nicxvan’s picture

I did confirm that the same error occurs before I changed it to a service as well, so it shouldn't be my changes.

berdir made their first commit to this issue’s fork.

berdir’s picture

Status: Needs work » Needs review

See my comments on the MR.

smustgrave’s picture

Status: Needs review » Needs work

Seems to still have some test failures unfortunately.

mohit_aghera made their first commit to this issue’s fork.

berdir’s picture

Issue summary needs an update, \Drupal\Core\Render\Element\File::valueCallback() is no longer updated because that's different and weird.

maybe needs a change record?

That makes this a bit less useful as there's fewer places to unify, but I guess still useful as we build on it in the related issue?

Wondering if it would be cleaner/easier to test if request would be passed in to the method instead of the indirection with the request stack, but I think this is pretty common for us and I'm probably influenced by a recent book I've read.

nicxvan changed the visibility of the branch 3555115-add-a-uploadedfilesextractor-main to hidden.

mohit_aghera’s picture

Issue summary: View changes

Thanks for the feedback @berdir

- I've added a change record here https://www.drupal.org/node/3591900
- Issue summary updated.

I saw the commits and the changes in \Drupal\Core\Render\Element\File::valueCallback() have been reverted, so something might be awful there.

but I think this is pretty common for us and I'm probably influenced by a recent book I've read.

Just curious, which book you are talking about?

mohit_aghera’s picture

Status: Needs work » Needs review
needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new665 bytes

The Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

smustgrave’s picture

Status: Needs work » Needs review
berdir’s picture

Issue summary: View changes
Status: Needs review » Reviewed & tested by the community

I updated the CR and issue summary a bit more, looks good to me.

> Just curious, which book you are talking about?

https://codethatships.com/

quietone’s picture

Issue summary: View changes
Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs change record updates

I read the comments, the MR and the change record. I have updated credit.

The last sentence of the change record refers to $form_field_name which is not used in the the change record. Is something missing from the change record? Setting to needs work for that.

mohit_aghera’s picture

Status: Needs work » Needs review

@quietone, this looks like a typo change.
I checked the diff here https://www.drupal.org/node/3591900/revisions/view/14360985/14364088 and it seems a typo.
I've updated the change record.

nicxvan’s picture

Status: Needs review » Reviewed & tested by the community

Thanks @quietone! Feedback has been addressed!