-
Notifications
You must be signed in to change notification settings - Fork 109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rename uploaded files using webform filename pattern (and tokens) #1009
Conversation
Test fails are unrelated. I'll pull in the PR and test it this week. Thank you @sluc23 👍 |
Yes i think saving files in the presave sounds fine to me borrowing few code from webform module. But i think the approach can be made better if we can do the following? Eg
Thoughts @sluc23 ? |
@jitendrapurohit I think it is a good idea too.. but in that case I think that refactor is much complex that what I'm able to achieve.. that's a bigger architecture modification. Creating a new service in the module will require more tests, check that does not affect other sections of the module source code, taking important decisions, etc.. In the case you want to move to that direction i can offer my self to review/test the solution, but not to code it.. |
I'm ok with the simpler approach here (for now). Thank you @sluc23 @jitendrapurohit did you have a chance the functionality? If yes - then when can merge this. |
yes have tested and it works as per the functionality is concerned. Merging. Thanks @sluc23 👍 |
if (isset($component['#file_name']) && $component['#file_name']) { | ||
$newFilename = $this->handler->tokenManager->replace($component['#file_name'], $this->submission); | ||
} | ||
if (empty($val[0]) || !($val = $this->saveDrupalFileToCivi($val[0], $newFilename))) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've just hit an error here with a vanilla install and I think this is the problem. It assumes we set the new Filename above. But I'm getting a warning "Undefined variable $newFilename " which suggests we didn't.
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