Closed (fixed)
Project:
Drupal core
Version:
11.x-dev
Component:
file.module
Priority:
Normal
Category:
Task
Assigned:
Unassigned
Issue tags:
Reporter:
Created:
19 Jul 2023 at 05:18 UTC
Updated:
15 Sep 2026 at 16:20 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #2
kim.pepperComment #3
andypostThank you fur schemas!
Comment #4
larowlanThis feels like a good first step and is compatible with #3221796: [META] Modernise file upload logic
Comment #6
kim.pepperThis deprecates:
file_managed_file_save_upload()_file_save_upload_from_form()file_save_upload()and replaces them with a few new services.
\Drupal\file\Upload\FileElementHelper::saveFileUploads(array $element, FormStateInterface $formState)replacesfile_managed_file_save_upload($element, FormStateInterface $form_state)and_file_save_upload_from_form(). The intention is to separate all the form api and user-facing messaging into this service.\Drupal\file\Upload\FormFileUploadHandler::saveFileUploads(string $uploadKey, array $validators, callable $errorHandler, ?string $destination = 'temporary://', string $replace = FileSystemInterface::EXISTS_RENAME)replacesfile_save_upload($form_field_name, $validators = [], $destination = FALSE, $delta = NULL, $replace = FileSystemInterface::EXISTS_RENAME).callable $errorHandlerthat accept three arguments:\Drupal\file\Upload\UploadedFileInterface $uploadedFilestring $destination\Exception $eWe then have a
\Drupal\file\Upload\MessageCollectingErrorCallbackwhich we pass to collect any errors and allows us to remove a load of the messy message deleting, and re-adding that we had in_file_save_upload_from_form()\Drupal\file\Upload\FormUploadedFileRetrieverremoves the duplicated code of retrieving the uploaded files from the request, and is injected intoFileElementHelperandFormFileUploadHandler.Lastly, in order to not have to add a message for each file rename in our API-level code, instead we dispatch a
\Drupal\file\Upload\FileUploadedEventand added aMessengerFileUploadedSubscriberto add a status message if a file was renamed.Comment #7
kim.pepperI'm confused by the phpstan failure.
First it says its comparing against 10.2.x where the MR is for 11.x. Then its showing an error about a variable
$file_uploadwhich I couldn't find anywhere infile.module.Edit:
Running
./core/scripts/dev/commit-code-check.sh --branch 11.xlocally produces no errors.Comment #8
kim.pepperComment #9
kim.pepperLooks like I have an issue somewhere with setting the form errors and them being double-escaped:
Comment #10
smustgrave commentedSeems to have some test failures.
@larowlan as a committer do you agree with the task being done?
Comment #11
kim.pepperUpdating title and remaining tasks:
Comment #12
kim.pepperComment #13
kim.pepperThe image test fail is due to changes in the messages being displayed:
Old:
New:
Comment #14
kim.pepperFinally, tests passing! 😅
Comment #15
kim.pepperUpdated the mermaid diagram to match the current MR.
Comment #16
kim.pepperUpdated IS and CR
Comment #17
kim.pepperComment #18
joachim commentedThis no longer tells you the filename of the problem file. If you uploaded several, how are you supposed to know which one to fix?
Comment #19
kim.pepperI fixed that. There is no difference between HEAD and this MR now as to what is output.
Comment #20
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".
This does not mean that the patch 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.
Comment #21
kim.pepperRebase with 11.x
Comment #22
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".
This does not mean that the patch 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.
Comment #23
kim.pepperMerge with 11.x
Comment #24
kim.pepperPostponed on #3375447: Create an UploadedFile validator and deprecate error checking methods on UploadedFileInterface since we can remove some of the exception handling once that is in.
Comment #25
kim.pepperComment #26
larowlanBlocker is in
Comment #27
larowlanComment #28
kim.pepperGoing to try and avoid the whole 'message collector' business, and just return a new
FileResults(plural) object that has aConstraintViolationList. Might be a nice utility to write violations toDrupal::addMessage()or something like that.Comment #29
larowlanRemoving tag until #28 is done
Comment #30
kim.pepperFinally tests back to green. ✅ Ready for reviews again.
Comment #31
smustgrave commentedHiding files from the bot.
Removing tests tag as coverage appears to be there for the deprecations here https://git.drupalcode.org/issue/drupal-3375423/-/jobs/1646261
Did leave a few comments on the MR.
Comment #32
andypostbtw deprecations should be updated to 11.0.0 for removal in 12.0.0
Comment #33
kim.pepperAccording to @larowlan we should be deprecating in 10.4.0 for removal in 12.0.0. https://drupal.slack.com/archives/C1BMUQ9U6/p1716324332447859?thread_ts=...
Comment #34
kim.pepperComment #35
kim.pepperUpdated all deprecation versions to
drupal:10.4.0 and is removed from drupal:12.0.0Comment #36
andypostI find it ready now
Comment #37
larowlanCouple of questions on the MR, nice work!
Comment #38
kim.pepperUpdated IS
Comment #39
smustgrave commentedRe-reviewing and appears all feedback has been addressed.
CR is straight forward too.
Comment #40
alexpottThis change feels like it is doing more that it should. It is added typehints which mean we have to consider multiple angles of BC. It is changing logic deep in security focussed code. This should not be necessary to achieve the aims of the issue.
Comment #41
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. 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.
Comment #42
kim.pepperThis got stalled most likely because it was trying to do too much.
I'm starting to split things off into smaller issues starting with #3555115: Add a UploadedFilesExtractor and remove duplicate code
Comment #43
nicxvan commentedComment #45
kim.pepperPropose converting this to a meta issue under #3221796: [META] Modernise file upload logic, since it stalled from combining too much scope. #3555115: Add a UploadedFilesExtractor and remove duplicate code already replaces
FormUploadedFileRetrieverwithUploadedFilesExtractor.Suggested child issues, in dependency order:
- Request-scoped memory cache for file uploads (no dependencies).
- FileUploadResult::getErrors() for FileExistsException and FileException; handler stops throwing them (no dependencies).
- New multi-file upload service returning array (depends on the two above).
- Deprecate file_save_upload() with a BC shim (depends on the new service).
- Migrate the ManagedFile widget and remove _file_save_upload_from_form() (depends on the new service).
- Migrate ThemeSettingsForm, ImportForm, and navigation SettingsForm (depends on the new service).
- Test cleanup: rewrite file_test, re-home SaveUploadFormTest assertions, convert Functional to Kernel tests where possible (depends on all above).
This replaces the FileElementHelper/FormFileUploadHandler design with smaller, independently reviewable issues.
I'll go ahead an start creating these sub-issues.
Comment #46
kim.pepperCreated sub-issues:
Comment #47
kim.pepperFlagging the IS needs updating with the new sub-task approach
Comment #52
kim.pepperSince @nicxvan worked on this PR overnight, I've done some cleanup. We can postpone the sub-issues I created if this approach is viable.
MemoryCacheInterfacetoBackendCacheInterfacefile:uploads:<field_name>).FileElementHelpertoManagedFileElementHelperto avoid confusion between the form element\Drupal\file\Element\ManagedFileand the render element\Drupal\Core\Render\Element\File, and updated all references.Comment #53
kim.pepperHere's a before/after diagram.
Comment #54
nicxvan commentedComment #55
nicxvan commentedI reviewed the changes @kim.pepper made and they all look good.
This could use a review, but one important note is this contains both:
#3616645: Form #value_callback does not support CallableResolver-style callables
#3616663: ElementInfoManager overwrites a form element's own #value_callback declaration
This is technically postponed on those two, but let's get this reviewed on top of those.
Comment #56
kim.pepperI reviewed this code (it has been a while) and I think we should remove as much user facing code from FormFileUploader and let calling code handle Messenger calls. This resulted in a new result value object and bringing in #3619417: Return FileExistsException and FileException as data on FileUploadResult into this MR.
FileUploadResult::addError()/getErrors()/hasErrors().FileUploadHandler::handleFileUpload()now returnsFileExistsExceptionandFileExceptionas data instead of throwing them.FormFileUploaderno longer depends on Messenger or the Renderer; it returns errors as data on the result object.Comment #57
nicxvan commentedI reviewed those changes, I think it's out of scope, had a short discussion in slack and pushed the MR with that change to #3619417: Return FileExistsException and FileException as data on FileUploadResult
I then reverted it here.
I think the open question is do we remove the renderer and messenger from DI since we're going to do the follow up quickly.
I think we should since we don't want those to be part of the api long term.
Comment #58
nicxvan commentedDid a closer review, the issues mentioned in the IS are not prerequisites.
#3616645: Form #value_callback does not support CallableResolver-style callables
#3616663: ElementInfoManager overwrites a form element's own #value_callback declaration
I just rebased to pull out the prerequisite commits and the exceptions.
I think the only remaining question is whether we need to remove DI for messenger and renderer.
Comment #60
kim.pepperI have reviewed the changes that @nicxvan has been making, and (one minor nitpick remaining) I think this is ready. RTBC+1
Comment #61
kim.pepperNote for committers: the API is pretty much a 1-to-1 conversion from the procedural code. @alexpott highlighted (somewhere?) the security risk of rewriting the whole API in one go, so I think we need to make these changes incrementally. Having said that, I'm keen to keep pushing forward on cleaning up remaining issues (e.g. the Messenger calls in what should be a pure API) in follow ups.
Comment #63
nicxvan commentedBroke some tests, let me fix that.
I've reviewed all of the changes that @kim.pepper made and I agree this is pretty much ready, but the last commit could use a review again.
Comment #64
nicxvan commentedStill a couple more.
Comment #65
kim.pepperThe last couple of commits we just updating the default
$destinationparam handling and a minor typehint change `int|null` -> `?int`. Looks good, so back to RTBCComment #66
nicxvan commentedThanks, yeah I had to change a couple of places to handle that change.
Comment #67
nicxvan commentedI've addressed all of the feedback again. I also checked if we could remove the autowire, we can't yet.
Comment #69
amateescu commentedCommitted 72844a3 and pushed to main. Thanks!
Needs a quick rebase on 11.x.
Comment #70
amateescu commentedDid the backport myself and committed e842449 and pushed to 11.x.