0

I have a webform with a file upload field. I need do one of two things. Either upload files into a subfolder inside the private area, OR, add a prefix to the filename that the user is uploading. The user can upload several files. The webform editor allows you to 'Rename' the file and use tokens for this, but I don't see any way of preserving the original file name. I can hack getFileDestinationUri() in WebformManagedFileBase.php to do what I want, but obviously I would rather not do that. Am I missing something?

1 Answer 1

1

It turns out that you can use the drupal form alter hook to alter the destination. I had thought of this but felt if was unlikely to work. It does.

Below is MY code solution: (I may be missing container types)

function _mymodule_fix_elements(&$elements) {
  foreach ($elements as $key => &$element) {
    if (strpos($key, '#') !== 0) {
      if (is_array($element)) {
        if (isset($element['#type'])) {
          if (($element['#type'] == 'fieldset') ||
              ($element['#type'] == 'webform_flexbox') ||
              ($element['#type'] == 'container')) {
            _mymodule_fix_elements($element);
          } else if ($element['#type'] == 'managed_file') {
            $pattern = $element['#upload_location'];
            if (strpos($pattern, 'private:') === 0) {
              $element['#upload_location'] = $pattern . '/' . $key;
            }
          }
        }
      }
    }
  }
}

function mymodule_form_alter(&$form, &$form_state, $id) {
  if (strpos($id, 'webform_') === 0) {
    _mymodule_fix_elements($form['elements']);
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.