Repository navigation
feat: replace the deprecated file upload functions with the FormFileUploader and ManagedFileElementHelper services (#433) - #434
Open
bbrala wants to merge 1 commit into
Conversation
file_save_upload(), file_managed_file_save_upload() and _file_save_upload_from_form() are deprecated in drupal:11.5.0 and removed in drupal:13.0.0 (https://www.drupal.org/i/3375423, change record https://www.drupal.org/node/3382414). Core moved each function body into a new service (drupal-core e8424495f0d) and left the function delegating to it: file_save_upload($name, $validators, $destination, $delta, $fileExists) -> \Drupal::service(\Drupal\file\Upload\FormFileUploader::class) ->saveFormUploadedFiles(...) file_managed_file_save_upload($element, $form_state) -> \Drupal::service(\Drupal\file\Upload\ManagedFileElementHelper::class) ->managedFileSaveUpload(...) _file_save_upload_from_form($element, $form_state, $delta, $fileExists) -> \Drupal::service(\Drupal\file\Upload\ManagedFileElementHelper::class) ->saveFileUploads(...) Not a FunctionToServiceRector config entry, because the wrapper also normalised its arguments where the service methods do not. file_save_upload() turned a FALSE/NULL $destination into 'temporary://' before delegating, and saveFormUploadedFiles() types that parameter string — a verbatim pass-through would hand the service a bool and fatal. That is the dominant contrib shape: a literal FALSE or NULL third argument appears in 47 files across the contrib index (tmgmt, file_replace, paragraphs_browser, bootstrap_italia, dxpr_theme, site_verify, micon, ...), so the rector rewrites the literal. Both functions also ran a legacy int $fileExists through FileExists::fromLegacyInt(), while the service parameters accept only the enum. A FileSystemInterface::EXISTS_* constant is already converted by the drupal-10.3 set, so the only shape left is a bare integer literal; such a call is skipped rather than rewritten into a TypeError. No contrib project passes one today. BC-wrapped: FormFileUploader and ManagedFileElementHelper arrive with the deprecation itself (git grep at e8424495f0d~1 finds neither), so the rewritten call fatals on Drupal < 11.5. Neither the deprecated functions nor the service methods take a parameter by reference, so the wrapper emits arrow functions, and all three methods return a value, so no phpstan function.void. Live-tested against D11-compatible contrib on core 11.x-dev: gin_login (27,767 installs), media_entity_file_replace (23,146), tmgmt (11,680), file_replace (10,008), paragraphs_browser (9,901), responsive_favicons (4,017), gutenberg (2,790), file_history — 8 call sites rewritten across all three functions, including the FALSE and NULL destination shapes and a call already using FileExists::Replace. plupload was a no-change: its only occurrence is an @see docblock, correctly left alone. Verified idempotent with importNames(true) — a second pass is a no-op. PHPSTAN_MESSAGES captured from those modules and normalised with scripts/normalize-phpstan-message.php; docs/coverage-registry.php regenerated.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #433.
Adds
ReplaceFileSaveUploadFunctionsRectorto the Drupal 11.5 deprecation set.file_save_upload(),file_managed_file_save_upload()and_file_save_upload_from_form()are deprecated in drupal:11.5.0 and removed in drupal:13.0.0 (#3375423, change record, coree8424495f0d).Why a custom rule and not two
FunctionToServiceRectorentriesCore moved each function body into the new service verbatim, so the argument order carries over — but the deprecated wrapper also normalised its arguments, which the service methods do not:
$destination.file_save_upload()turned aFALSE/NULL$destinationinto'temporary://'before delegating, andsaveFormUploadedFiles()types that parameterstring. A verbatim pass-through hands the service a bool and fatals with aTypeError. This is the dominant contrib shape — a literalFALSE/NULLthird argument appears in 47 files across the contrib index — so the rule rewrites the literal.$fileExists. Bothfile_save_upload()and_file_save_upload_from_form()ran a legacy int throughFileExists::fromLegacyInt(), while the service parameters accept only the enum. AFileSystemInterface::EXISTS_*constant is already converted by the drupal-10.3 set, so the only shape left is a bare integer literal; such a call is skipped rather than rewritten into aTypeError. No contrib project passes one today.Calls with named arguments, argument unpacking, or an argument count the deprecated functions never had are skipped as well.
BC
BC-wrapped.
FormFileUploaderandManagedFileElementHelperarrive with the deprecation commit itself —git grepate8424495f0d~1finds neither — so the rewritten call fatals on Drupal < 11.5. Neither the deprecated functions nor the service methods take a parameter by reference, so the wrapper emits arrow functions, and all three methods return a value, so there is no phpstanfunction.void.Verification
Live-tested against D11-compatible contrib on core 11.x-dev (version forced to 11.5.0, since core still reports
11.4-dev): 8 call sites rewritten across gin_login (27,767 installs), media_entity_file_replace (23,146), tmgmt (11,680), file_replace (10,008), paragraphs_browser (9,901), responsive_favicons (4,017), gutenberg (2,790) and file_history — covering all three functions and theFALSE,NULL, variable-destination andFileExists::Replaceargument shapes. plupload produced no change: its only occurrence is an@seedocblock, correctly left alone.Verified idempotent with
importNames(true)— a second pass is a no-op.PHPSTAN_MESSAGESwere captured from those modules and normalised withscripts/normalize-phpstan-message.php;docs/coverage-registry.phpis regenerated.Test suite (707 tests, 973 assertions), phpstan and php-cs-fixer are all green.