Skip to content

Commit

Permalink
FOIA-0: Fixes for XML upload via core patch. (#646)
Browse files Browse the repository at this point in the history
  • Loading branch information
brockfanning authored Aug 30, 2024
1 parent c22c0d5 commit 9262d61
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 2 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@
"FOIA-32: Dynamically provide action plugins for every moderation state change": "https://www.drupal.org/files/issues/2019-11-20/2797583_provide_moderation_states_as_actions_128.patch",
"FOIA-32: Exclude Agency/Component from validation for bulk publishing": "./patches/FOIA-32-validator.patch",
"2869592 - Disabled update module shouldn't produce a status report warning": "https://www.drupal.org/files/issues/2024-07-16/2869592-disabled-update-module-71.patch",
"2230909 - Simple decimals fail to pass validation": "https://www.drupal.org/files/issues/2024-06-04/2230909-321.patch"
"2230909 - Simple decimals fail to pass validation": "https://www.drupal.org/files/issues/2024-06-04/2230909-321.patch",
"Hacks related to file_entity to get XML upload to work": "./patches/managed-file-hacks-for-xml-upload.patch"
},
"drupal/password_policy": {
"Config install issues": "https://www.drupal.org/files/issues/2022-10-04/password_policy_field_last_password_reset_unknown_error_2771129-134.patch"
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
'file_validate_extensions' => ['xml'],
],
'#required' => TRUE,
'#file_type' => 'document',
];

$form['next_step'] = [
Expand Down
65 changes: 65 additions & 0 deletions patches/managed-file-hacks-for-xml-upload.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
diff --git a/core/modules/file/file.module b/core/modules/file/file.module
index 6fcfa8c789..e80041cd22 100644
--- a/core/modules/file/file.module
+++ b/core/modules/file/file.module
@@ -532,8 +532,9 @@ function _file_save_upload_from_form(array $element, FormStateInterface $form_st
$upload_location = $element['#upload_location'] ?? FALSE;
$upload_name = implode('_', $element['#parents']);
$upload_validators = $element['#upload_validators'] ?? [];
+ $file_type = $element['#file_type'] ?? FALSE;

- $result = file_save_upload($upload_name, $upload_validators, $upload_location, $delta, $fileExists);
+ $result = file_save_upload($upload_name, $upload_validators, $upload_location, $delta, $fileExists, $file_type);

// Get new errors that are generated while trying to save the upload. This
// will also clear them from the messenger service.
@@ -614,7 +615,7 @@ function _file_save_upload_from_form(array $element, FormStateInterface $form_st
*
* @see _file_save_upload_from_form()
*/
-function file_save_upload($form_field_name, $validators = [], $destination = FALSE, $delta = NULL, FileExists|int $fileExists = FileExists::Rename) {
+function file_save_upload($form_field_name, $validators = [], $destination = FALSE, $delta = NULL, FileExists|int $fileExists = FileExists::Rename, $file_type = FALSE) {
if (!$fileExists instanceof FileExists) {
// @phpstan-ignore-next-line
$fileExists = FileExists::fromLegacyInt($fileExists, __METHOD__);
@@ -657,7 +658,7 @@ function file_save_upload($form_field_name, $validators = [], $destination = FAL
foreach ($uploaded_files as $i => $uploaded_file) {
try {
$form_uploaded_file = new FormUploadedFile($uploaded_file);
- $result = $file_upload_handler->handleFileUpload($form_uploaded_file, $validators, $destination, $fileExists, FALSE);
+ $result = $file_upload_handler->handleFileUpload($form_uploaded_file, $validators, $destination, $fileExists, FALSE, $file_type);
if ($result->hasViolations()) {
$errors = [];
foreach ($result->getViolations() as $violation) {
diff --git a/core/modules/file/src/Upload/FileUploadHandler.php b/core/modules/file/src/Upload/FileUploadHandler.php
index 9f3661871f..cc011f399a 100644
--- a/core/modules/file/src/Upload/FileUploadHandler.php
+++ b/core/modules/file/src/Upload/FileUploadHandler.php
@@ -174,7 +174,7 @@ public function __construct(
* @throws \ValueError
* Thrown if $fileExists is a legacy int and not a valid value.
*/
- public function handleFileUpload(UploadedFileInterface $uploadedFile, array $validators = [], string $destination = 'temporary://', /*FileExists*/$fileExists = FileExists::Replace, bool $throw = TRUE): FileUploadResult {
+ public function handleFileUpload(UploadedFileInterface $uploadedFile, array $validators = [], string $destination = 'temporary://', /*FileExists*/$fileExists = FileExists::Replace, bool $throw = TRUE, $file_type = FALSE): FileUploadResult {
if (!$fileExists instanceof FileExists) {
// @phpstan-ignore-next-line
$fileExists = FileExists::fromLegacyInt($fileExists, __METHOD__);
@@ -267,11 +267,16 @@ public function handleFileUpload(UploadedFileInterface $uploadedFile, array $val
);
}

- $file = File::create([
+ $file_values = [
'uid' => $this->currentUser->id(),
'status' => 0,
'uri' => $uploadedFile->getRealPath(),
- ]);
+ ];
+ if ($file_type) {
+ $file_values['type'] = $file_type;
+ }
+
+ $file = File::create($file_values);

// This will be replaced later with a filename based on the destination.
$file->setFilename($filename);

0 comments on commit 9262d61

Please sign in to comment.