From 9262d61a77ab261ab201662225206c76e6917d1d Mon Sep 17 00:00:00 2001 From: brockfanning Date: Fri, 30 Aug 2024 13:49:27 -0400 Subject: [PATCH] FOIA-0: Fixes for XML upload via core patch. (#646) --- composer.json | 3 +- composer.lock | 2 +- .../src/Form/AgencyXmlUploadForm.php | 1 + .../managed-file-hacks-for-xml-upload.patch | 65 +++++++++++++++++++ 4 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 patches/managed-file-hacks-for-xml-upload.patch diff --git a/composer.json b/composer.json index 03341153c..f0841b619 100644 --- a/composer.json +++ b/composer.json @@ -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" diff --git a/composer.lock b/composer.lock index 1c91a79f2..97a4772ee 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "da0e5cb202e23ec3175c0d8a6f14570c", + "content-hash": "4869699dfeeb000f97e5c0bc2f92758a", "packages": [ { "name": "acquia/blt", diff --git a/docroot/modules/custom/foia_upload_xml/src/Form/AgencyXmlUploadForm.php b/docroot/modules/custom/foia_upload_xml/src/Form/AgencyXmlUploadForm.php index f1debf55f..d3e14ddf5 100644 --- a/docroot/modules/custom/foia_upload_xml/src/Form/AgencyXmlUploadForm.php +++ b/docroot/modules/custom/foia_upload_xml/src/Form/AgencyXmlUploadForm.php @@ -75,6 +75,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { 'file_validate_extensions' => ['xml'], ], '#required' => TRUE, + '#file_type' => 'document', ]; $form['next_step'] = [ diff --git a/patches/managed-file-hacks-for-xml-upload.patch b/patches/managed-file-hacks-for-xml-upload.patch new file mode 100644 index 000000000..15ea05a12 --- /dev/null +++ b/patches/managed-file-hacks-for-xml-upload.patch @@ -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);