Drupal 8 Drupal 8 Webform为上传的文件名添加前缀

Drupal 8 Drupal 8 Webform为上传的文件名添加前缀,drupal-8,drupal-webform,Drupal 8,Drupal Webform,我有一个带有文件上传字段的网络表单。我需要做两件事中的一件。将文件上载到专用区域内的子文件夹中,或者在用户正在上载的文件名中添加前缀。用户可以上传多个文件。webform编辑器允许您“重命名”文件并为此使用标记,但我看不到任何保留原始文件名的方法。我可以在WebformManagedFileBase.php中破解getFileDestinationUri()来做我想做的事情,但显然我不想这么做。我遗漏了什么吗?事实证明,您可以使用drupal表单alter hook来更改目的地。我曾想过这一点,

我有一个带有文件上传字段的网络表单。我需要做两件事中的一件。将文件上载到专用区域内的子文件夹中,或者在用户正在上载的文件名中添加前缀。用户可以上传多个文件。webform编辑器允许您“重命名”文件并为此使用标记,但我看不到任何保留原始文件名的方法。我可以在WebformManagedFileBase.php中破解getFileDestinationUri()来做我想做的事情,但显然我不想这么做。我遗漏了什么吗?

事实证明,您可以使用drupal表单alter hook来更改目的地。我曾想过这一点,但觉得不太可能奏效。是的

下面是我的代码解决方案:(我可能缺少容器类型)

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']);
  }
}