Allow administrator to chose upload folder for files

Created on 8 February 2018, almost 7 years ago
Updated 7 February 2023, almost 2 years ago

Currently when a file is uploaded the webform module will automatically add it in a folder named after the submission ID, under private:webform/WEBFORM_KEY.

So, you end up with a structure like:

$ tree
.
├── 301
│   └── file_name.jpg
├── 302
│   └── file_name.jpg
├── 303
│   └── file_name.jpg
├── 304
│   ├── file_name1.jpg
│   ├── file_name2.jpg
│   └── file_name3.jpg
└── _sid_

I suggest to give the option to admins to decide whether they want this structure or an option to save all files under one folder, without the subfolders.
With #2941286: Rename file uploads for v8.x commited we have control over the filename anyway, so using subfolders for every submission might not be needed for all projects.

Feature request
Status

Closed: works as designed

Version

5.0

Component

Code

Created by

🇬🇷Greece bserem

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.

  • 🇫🇮Finland MikaT

    Note that the custom properties doesn't seem to support tokens so one might need to extend the WebformManagedFileBase and in that new class override the getUploadLocation.

    As I needed token support, I did it so that I created new WeformElement extending the WebformManagedFileBase that introduced new field where the upload location can be entered. Then in getUploadLocation get the new field value from $element, run $this->replaceTokens($element) and then call the parent getUploadLocation.

    
    namespace Drupal\my_module\Plugin\WebformElement;
    
    use Drupal\Core\Form\FormStateInterface;
    use Drupal\webform\Plugin\WebformElement\WebformManagedFileBase;
    use Drupal\webform\WebformInterface;
    
    /**
     * Custom version of the webform document file.
     *
     * @WebformElement(
     *   id = "custom_webform_managed_file",
     *   label = @Translation("Custom managed file"),
     *   description = @Translation("Custom version of the webform managed file."),
     *   category = @Translation("File upload elements"),
     *   states_wrapper = TRUE,
     *   dependencies = {
     *     "file",
     *   }
     * )
     */
    class CustomManagedFile extends WebformManagedFileBase {
    
      /**
       * {@inheritdoc}
       */
      protected function defineDefaultProperties() {
        return parent::defineDefaultProperties() + [
          'file_directory' => '',
        ];
      }
    
      /**
       * {@inheritdoc}
       */
      public function form(array $form, FormStateInterface $form_state) {
        $form = parent::form($form, $form_state);
    
        $form['file']['file_directory'] = [
          '#type' => 'textfield',
          '#title' => $this->t('File directory'),
          '#description' => $this->t('Optional subdirectory within the upload destination where files will be stored. Do not include preceding or trailing slashes.'),
        ];
    
        return $form;
      }
    
      /**
       * {@inheritdoc}
       */
      protected function getUploadLocation(array $element, WebformInterface $webform) {
        if (empty($element['#file_directory'])) {
          return parent::getUploadLocation($element, $webform);
        }
        $uriScheme = $this->getUriScheme($element);
        // Replace tokens.
        $this->replaceTokens($element);
        // Alter the element so we can call parent.
        $element['#upload_location'] = $uriScheme . '://' . $element['#file_directory'];
    
        return parent::getUploadLocation($element, $webform);
      }
    
    }
    
    

    Remember to create an element matching the ID of the webform element

    <?php
    
    namespace Drupal\ssp_site\Element;
    
    use Drupal\webform\Element\WebformManagedFileBase;
    
    /**
     * Provides a webform element for an 'custom_webform_managed_file' element.
     *
     * @FormElement("custom_webform_managed_file")
     */
    class CustomManagedFile extends WebformManagedFileBase {
    
    }
    
    
Production build 0.71.5 2024