1

Currently Webform file component uses public files as default upload destination. This is probably because private files path is not always available if not configured in Drupal settings correctly.

File upload destination should be made automatically "private files" by default if available.

I found same issue with Drupal7, is there any patch or way for Drupal8?

Or better if we do it by some setting in the local.settings.php or some similar cofniguraions?

https://www.drupal.org/forum/support/upgrading-drupal/2012-10-08/changing-public-file-field-into-private

3
  • Can you go to /admin/config/media/file-system and set "Private local files served by Drupal." as the default file system storage?
    – 100pic
    Commented Apr 11, 2021 at 22:50
  • I did it, but it works for new fields that will be created, old fields still need to going and updating them relatively. @100pic Commented Apr 12, 2021 at 5:23
  • @NasserAliKarimi Yes, whenever i have to change locations, ive always made a new field name and migrated to it. Either that or write a script that will resave all of your fields. Commented Apr 12, 2021 at 20:49

1 Answer 1

0

With file_save_data being removed from Drupal 10, this is how I now save a file upload to my file field type. $docs is the array that my decoupled Formik form provides on form submit and I had to extract the data from the array and decode. I then get the file id to put into the file field on the node. Probably a better way, but I made this work.

/**
   * @param $docs
   * Save all doc attachments to the private directory.
   *
   * @throws \Drupal\Core\Entity\EntityStorageException
   */
  public function saveDocuments($docs): void {
    foreach ($docs as $key => $dataIn) {
      // Save the plate with the filename for audit trail.
      $this->fileName = $this->license . '-' . $dataIn['filename'];
      $data = $dataIn['file_data'];
      [$type, $data] = explode(';', $data);
      [, $data]      = explode(',', $data);
      $data = base64_decode($data);

      /** @var \Drupal\file\FileRepositoryInterface $fileRepository */
      $fileRepository = \Drupal::service('file.repository');
      $uri = "private://" . $this->fileName;
      $fileEntity = $fileRepository->writeData($data, $uri, FileSystemInterface::EXISTS_REPLACE);
      $this->documents[] = $fileEntity->id();

    }
  }

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.