Allow minimal token support for upload location

Created on 10 December 2020, over 4 years ago
Updated 4 July 2023, almost 2 years ago

Please add token support in upload_location

When uploading files to the temporary location, f.e. via a media form, but the entity does NOT get saved (meaning it does also not get moved by the module), the uploaded file will remain in the temporary directory. This leads to renaming of newly uploaded files that have the same name as the original, because there is already a file present with that name in the temporary location. So newly uploaded files will be renamed with a suffix like "_0". Sadly, also the moved files after saving the new entity will get that suffix, too.

To prevent that, the temporary directory could support tokens, to f.e. write the uploaded files to subdirectories based on timestamps. This would prevent the renaming of files on upload, if prior uploads were not successfull or submitted.

function filefield_paths_field_widget_form_alter(&$element, FormStateInterface $form_state, $context) {
// Force all File (Field) Paths uploads to go to the temporary file system
// prior to being processed.
if (isset($element['#type']) && $element['#type'] == 'managed_file') {
$settings = $context['items']->getFieldDefinition()
->getThirdPartySettings('filefield_paths');
if (isset($settings['enabled']) && $settings['enabled']) {
$element['#upload_location'] = \Drupal::token()->replace(\Drupal::config('filefield_paths.settings')->get('temp_location')); //SD
}
}
}

Remaining tasks

User interface changes

API changes

Data model changes

Feature request
Status

Active

Version

1.0

Component

Code

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024