- 🇫🇮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 thegetUploadLocation
.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 { }