Hi there,

I think it could be useful to be able to manage 'file' field name via tokens

Let say youre creating a job appliance webform. The user must upload its curriculum vitae. With a feature like this one, you could rename each file with the name+timestamp

I can be done using 'entity forms' and 'FileField paths' ...

What do you think of this request ? do-able ?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

waluyo.umam’s picture

I am also searching how to rename the uploaded file using the tokens (from the submssion values).
Any possiblity to rename the uploaded file?

DanChadwick’s picture

Version: 7.x-4.0-beta1 » 7.x-4.x-dev

This sounds like a reasonable request, but I don't plan to code it. Patches are welcome.

miteshmap’s picture

FileSize
1.99 KB

Here is a custom module developed to give the functionality to rename the file name, with other component tokens.

created at Coworks

miteshmap’s picture

FileSize
2.03 KB

Updated a minor code to handle the URI.

Check this on sandbox - https://www.drupal.org/sandbox/developermitesh/2384305

created at Coworks

DanChadwick’s picture

@developermitesh -- I haven't reviewed your module yet, but if you made this a patch against 7.x-4.x, I would be happy to review it for including in webform. :) I think it is a reasonable feature.

miteshmap’s picture

@DanChadwick -- thanks for the suggestion, Here's a patch, that rename the uploaded file based on the tokens of other component. I would be really happy if you can review it and include it to webform :)

miteshmap’s picture

Status: Active » Needs review
DanChadwick’s picture

Status: Needs review » Needs work
Issue tags: -file path tokens

Thanks for the patch. Great work. I didn't try it yet, but here are some things I noticed:

1) We certainly can't have an English list of words to ignore in title. I'm not sure what the best approach here is. If we put them into a t('xxx') string, they could be translated, but I can also imagine wanting multiple lists to be used at once. For example, if in my English site, there is a filename "La Siesta", you might want remove "La ". One compromise solution might be something like:

variable_get('webform_filename_ignore_words', t('a an ...'));

This would allow them to be translated or customized by a user in the settings.php file.

I think the format with just a single space between each word would be easier to deal with. You could explode to turn it into an array, prepare it for use in the regex, then generate the regex with implode.

2) When generating regex strings dynamically, use preg_quote to protect from regex characters in the string. This might not apply to your static list of words, but it would to a dynamic one.

3) The description for the form is a bit awkward. You have "Rename the file with tokens (please don\'t include extension, leave empty if you don\'t want to rename), Supports Webform token replacements.". Maybe "Rename the file using tokens to create a pattern. Don't include an extension; it will be added automatically."

4) I think there should be a checkbox for this which reveals the rename option (much like how the preview options work). This doesn't have to be stored in the database -- an empty pattern can still be used for "no rename"). This would hide the complexity from those who don't want to use it.

5) The PHP function trim takes a character mask string, so I don't see why you have an ltrim, rtrim, and trim right in a row. A single trim with a character mask should do, no?

6) The dockblock needs to follow the Drupal standard, with a blank line before the parameters and with an additional row for each parameter, indented 2 spaces, which defines the parameter. The type should also be included (we are slowly transitioning webform to do this). Also, the docblock summary should be third-person singlar. "Renames the uploaded..." for example.

7) Why store the return value from file_move if you aren't going to use it?

This is a promising patch. The above issues are all easy to fix, except 1).

miteshmap’s picture

Status: Needs work » Needs review
FileSize
6.01 KB

Thanks very much for the quick review and comments. :)

1) as of now just added a textarea to add the wordlist to ignore like you said with space and on "Webform settings" page and could be configured with settings.php, because it is now using variable_get.

2) use preg_quote to protect from regex, I am not sure the place you are referring at. but the all preg_replace is called with regex pattern, so we may not use there, and for $ignore_words_regex we are using \b with each words. so, we may not use that with those words (I tried but didn't work out as expected.)

Rest of the comments are followed and made necessary changes.

DanChadwick’s picture

Status: Needs review » Needs work

Thanks for the revision.

1) I think it would be best to NOT have a UI for setting the ignore words. Webform is such a big and popular module, that we have to work hard to keep it as simple as possible. I'd prefer to leave this as a "hidden" feature, which can be configured with settings.php or the database.

Also, the list needs to run through translation -- i.e. t(...).

2) Because it is possible that the ignore words might themselves contain regex special characters (e.g. a period), they should be run through preg_quote. Where possible, str_replace should be used in preference to preg_replace. Alas, str_ireplace can't be relied upon for UTF8 case insensitivity.

3) I wonder if the transliteration module should be used (if available) rather than removing all non-ASCII alphanumeric characters.

I'm a bit +/- on adding the ignore words. I can see the benefit, and it relates tangentially for the request for a natural sort feature. OTOH, it complicates this token feature quite a bit. Do you think it could be removed without seriously diminishing the value of the token feature?

miteshmap’s picture

I understood your purpose, and to finalise the patch,
If we go with the 3rd point, Can we simply use the
dependency on "transliteration" for the "Webform file rename" feature, and
use the function - (transliteration_clean_filename) to clean the name with
translation ?

Like we have a checkbox "Rename file name after upload" If user check this
checkbox. we validate module_exists for "transliteration" and it gives an
error message of dependency. if you want to use this feature you need to
download "transliteration" module.?

Otherwise, if we choose to go with step 1 and 2 then we need to do
modification in "webform_file_rename_clean_name" function.

What will be your preferable way ? Waiting for your suggestion on this. :)

miteshmap’s picture

Status: Needs work » Needs review
DanChadwick’s picture

Status: Needs review » Needs work

To move forward, let's:

1) Remove the ignore words functionality.

2) Rely on the transliteration module for cleaning up special characters. That's what the module is for, after all. Unless I'm missing something.

miteshmap’s picture

Status: Needs work » Needs review
FileSize
5.52 KB

Above Mentioned points has been solved,
1) Removed the ignore words functionality.
2) Renaming of the filename is now dependent on transliteration module

Status: Needs review » Needs work

The last submitted patch, 14: webform_file_rename-2139287-14.patch, failed testing.

miteshmap’s picture

Status: Needs work » Needs review
FileSize
3.95 KB

Status: Needs review » Needs work

The last submitted patch, 16: webform_file_rename-2139287-14.patch, failed testing.

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 16: webform_file_rename-2139287-14.patch, failed testing.

miteshmap’s picture

Status: Needs work » Needs review
FileSize
3.96 KB

Status: Needs review » Needs work

The last submitted patch, 21: webform_file_rename-2139287-14.patch, failed testing.

miteshmap’s picture

Status: Needs work » Needs review
FileSize
3.63 KB

refactored to use webform's function "_webform_safe_name" to rename the file name

DanChadwick’s picture

Status: Needs review » Needs work

Re #23 -- Ha. Leave it to quicksketch to already use transliteration, if available. I haven't tried it, but reading the patch it looks pretty good.

I think we need a bit of error checking. If someone uses a pattern with no token, they will get a bunch of nearly-identical filenames. token_scan seems like it might do the trick. The pattern should also be trimmed, to remove leading/trailing white space, I think.

I appreciate the work. It looks close.

miteshmap’s picture

1) token_scan used with #element_validate with rename field, so, now on saving a field it validates the string.
2) Pattern is already being trimmed with in this code

$rename = webform_replace_tokens($component['extra']['rename'], $node, $submission, NULL, TRUE);
    $new_file_name = _webform_safe_name($rename);

Where the $rename will be string with replaced token which might have leading / trailing white space, with this function "_webform_safe_name" It first do remove the space with "trim", that way our goal is achieved. :)

Looking forward to complete this patch. :)

miteshmap’s picture

Status: Needs work » Needs review
DanChadwick’s picture

Status: Needs review » Needs work
FileSize
6.93 KB

I pushed this forward a bit. This patch does:

1) Removed use_rename, leaving a blank rename field as the indication that no rename should take place
2) Various re-wordings of user-visible text and messages
3) Cleaning and validation of the pattern
4) Allow the pattern to have uppercase, space, and dash characters (as filenames do now).
5) Various comment and code style tweaks.

What's left:
1) Renaming happens at every database save, meaning that the file may change names with changing submission values. I don't think this is good.
2) Some useful tokens, such as [submission:sid] aren't available currently because the rename is happening too early.

I think (but am not totally sure that) the solution is to defer the rename to the point where the insert and update routines (rather than presave), and be sure to perform the rename only once, when the submission is first finalized (i.e. not a draft).

  • DanChadwick committed efc74ce on 7.x-4.x
    Issue #2139287 by developermitesh, DanChadwick: Renaming 'File' uploads...
DanChadwick’s picture

Status: Needs work » Fixed

This patch:

  1. Add the rename option, with token browser and tweaked UI verbiage.
  2. Records while components / fids need renaming during the pre-save operation.
  3. Only renames upon the first final submission save. Drafts and re-saving existing final submissions don't cause renaming. In this way, the file is only renamed once.
  4. The renamed file uses the new token directory.
  5. The renaming code was moved from webform.module to file.inc
  6. The transliteration code was split out of webform_safe_name.

Note that due to the timing, the directory cannot use submission tokens, but the filename can.

Committed to 7.x-4.x and 8.x.

  • DanChadwick committed d030422 on 8.x-4.x
    Issue #2139287 by developermitesh, DanChadwick: Renaming 'File' uploads...
msmorais’s picture

I applied the patch and everything is working normally, but I have hidden fields with conditionals and after pressing submit button appears 'Notice: Undefined offset: 0 in webform submission webform presave () on line 1228' for each hidden field

I think this code would solve.

if(!empty($submission->data[$cid][0])){
   if (strlen($component['extra']['rename'])) {
	webform_file_process_rename($node, $submission, $component, $submission->data[$cid][0]);
   }
}
miteshmap’s picture

@msmorais, Sorry to inform you that the above patches lacks some last minute code changes, so, I would recommend you to use this patch finalised and committed by DanChadwick - http://cgit.drupalcode.org/webform/patch/?id=efc74cefbc22ce6183020051869...
Or you can use the dev version - 7.x-4.x-dev

msmorais’s picture

installed the dev version 7.x-4.x-dev and it worked, thank you.

ashopin’s picture

Will this work on 7.x-3.x? Unfortunately, we have an existing site that doesn't like 7.x-4.x.

DanChadwick’s picture

Re #34. No.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

amaisano’s picture

Version: 7.x-4.x-dev » 7.x-4.12
Status: Closed (fixed) » Active

Sorry to re-open this, but it doesn't work when continuing a previously saved-without-a-file submission (draft). All tokens are ignored completely when returning to an existing submission and *then* uploading a file, regardless of whether it's an anonymous user or an admin.

RoSk0’s picture

Status: Active » Closed (fixed)

@amaisano - You should open a bug report for your issue.

Gold’s picture

Version: 7.x-4.12 » 7.x-4.x-dev
Status: Closed (fixed) » Active

The code in patch #27 appears to be in the dev branch, but this issue is still present. At least it is for me. Is anyone else still seeing this?

Gold’s picture

Status: Active » Closed (fixed)

My bad. This is actually working. As it happens the field I was accessing lived in a fieldset so needed to be prefixed with that fieldsets machine name in the token.