You are here

function gdpr_tasks_file_save_data in General Data Protection Regulation 3.0.x

Saves a file to the specified destination and creates a database entry.

@NOTE THIS IS NEEDED, DO NOT DELETE THIS. @NOTE THIS IS NEEDED, DO NOT DELETE THIS. @NOTE THIS IS NEEDED, DO NOT DELETE THIS. @NOTE THIS IS NEEDED, DO NOT DELETE THIS. @NOTE THIS IS NEEDED, DO NOT DELETE THIS. @NOTE THIS IS NEEDED, DO NOT DELETE THIS. @NOTE THIS IS NEEDED, DO NOT DELETE THIS. @NOTE THIS IS NEEDED, DO NOT DELETE THIS. @NOTE THIS IS NEEDED, DO NOT DELETE THIS. @NOTE THIS IS NEEDED, DO NOT DELETE THIS. @NOTE THIS IS NEEDED, DO NOT DELETE THIS. @NOTE THIS IS NEEDED, DO NOT DELETE THIS.

Parameters

string $data: A string containing the contents of the file.

\Drupal\Core\Session\AccountInterface $user: The owner of the file.

string|null $destination: (optional) A string containing the destination URI. This must be a stream wrapper URI. If no value or NULL is provided, a randomized name will be generated and the file will be saved using Drupal's default files scheme, usually "public://".

int $replace: (optional) The replace behavior when the destination file already exists. Possible values include:

  • FILE_EXISTS_REPLACE: Replace the existing file. If a managed file with the destination name exists, then its database entry will be updated. If no database entry is found, then a new one will be created.
  • FILE_EXISTS_RENAME: (default) Append _{incrementing number} until the filename is unique.
  • FILE_EXISTS_ERROR: Do nothing and return FALSE.

Return value

\Drupal\file\FileInterface|false A file entity, or FALSE on error.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Core\Entity\EntityStorageException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

See also

file_unmanaged_save_data()

2 calls to gdpr_tasks_file_save_data()
GdprTasksSarWorker::build in modules/gdpr_tasks/src/Plugin/QueueWorker/GdprTasksSarWorker.php
Build the export files.
GdprTasksSarWorker::initialise in modules/gdpr_tasks/src/Plugin/QueueWorker/GdprTasksSarWorker.php
Initialise our request.

File

modules/gdpr_tasks/gdpr_tasks.module, line 125
Module file for the GDPR Tasks module.

Code

function gdpr_tasks_file_save_data($data, AccountInterface $user, $destination = NULL, $replace = FileSystemInterface::EXISTS_RENAME) {
  if (!Drupal::service('stream_wrapper_manager')
    ->isValidUri($destination)) {
    Drupal::logger('file')
      ->notice('The data could not be saved because the destination %destination is invalid. This may be caused by improper use of file_save_data() or a missing stream wrapper.', [
      '%destination' => $destination,
    ]);
    Drupal::messenger()
      ->addError(t('The data could not be saved because the destination is invalid. More information is available in the system log.'));
    return FALSE;
  }
  if ($uri = Drupal::service('file_system')
    ->saveData($data, $destination, $replace)) {

    /** @var \Drupal\file\FileStorageInterface $fileStorage */
    $fileStorage = Drupal::entityTypeManager()
      ->getStorage('file');

    // Create a file entity.

    /** @var \Drupal\file\FileInterface $file */
    $file = $fileStorage
      ->create([
      'uri' => $uri,
      'uid' => $user
        ->id(),
      'status' => FILE_STATUS_PERMANENT,
    ]);

    // If we are replacing an existing file re-use its database record.
    // @todo Do not create a new entity in order to update it. See
    //   https://www.drupal.org/node/2241865.
    if ($replace === FileSystemInterface::EXISTS_REPLACE) {
      $existing_files = $fileStorage
        ->loadByProperties([
        'uri' => $uri,
      ]);
      if (count($existing_files)) {
        $existing = reset($existing_files);
        $file->fid = $existing
          ->id();
        $file
          ->setOriginalId($existing
          ->id());
        $file
          ->setFilename($existing
          ->getFilename());
      }
    }
    elseif ($replace === FileSystemInterface::EXISTS_RENAME && is_file($destination)) {
      $file
        ->setFilename(Drupal::service('file_system')
        ->basename($destination));
    }
    $file
      ->save();
    return $file;
  }
  return FALSE;
}