Rename uploaded files using webform filename pattern (and tokens) #1009
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.
Overview
Webform hast the feature of setting specific filenames for
File
type fields. This allows to rename in the server uploaded files with specific names or using tokens. Description hereThis feature does not work in Webform CiviCRM, the uploaded files are stored in with the original filename.. why? because how processes are executed in this order:
private://
location with original filenamewebform_civicrm
handler is executed first and and copies the filename from the first private location tofiles/civicrm/custom
with the original name.ref: https://github.com/colemanw/webform_civicrm/blob/6.2.5/src/WebformCivicrmBase.php#L782
ref: https://git.drupalcode.org/project/webform/-/blob/6.2.2/src/Plugin/WebformElement/WebformManagedFileBase.php?ref_type=tags#L1217
This is where webform calculates the final filename based on this pattern:
ref: https://git.drupalcode.org/project/webform/-/blob/6.2.2/src/Plugin/WebformElement/WebformManagedFileBase.php?ref_type=tags#L1249
So we need this feature to work in
webform_civicrm
, and after some research I found this solutionBefore
Uploaded files are always saved with the original local name
After
Uploaded files are saved, following the filename pattern defined in the field settings
Technical Details
This is a solution I came up with, it could be better alternatives, but basically I had to copy how to calculate the filename from filename pattern in webform module.
CivicrmWebformHandler
needs to have a reference to thewebform.token_manager
service to do the calculation of filename.. this could be beneficial for using Drupal tokens in other sections of the module's handlerComments
Open to suggestions