function ManagedFileElementHelper::saveFileUploads

Saves form file uploads.

The files will be added to the {file_managed} table as temporary files. Temporary files are periodically cleaned. Use the 'file.usage' service to register the usage of the file which will automatically mark it as permanent.

Parameters

array $element: The FAPI element whose values are being saved.

\Drupal\Core\Form\FormStateInterface $formState: The current state of the form.

null|int $delta: (optional) The delta of the file to return the file entity. Defaults to NULL.

\Drupal\Core\File\FileExists $fileExists: (optional) The replace behavior when the destination file already exists.

Return value

array|\Drupal\file\FileInterface|null|false An array of file entities or a single file entity if $delta != NULL. Each array element contains the file entity if the upload succeeded or FALSE if there was an error. Function returns NULL if no file was uploaded.

See also

https://www.drupal.org/project/drupal/issues/3069020

https://www.drupal.org/project/drupal/issues/2482783

File

core/modules/file/src/Upload/ManagedFileElementHelper.php, line 68

Class

ManagedFileElementHelper
Helper class for ManagedFile element form file uploads.

Namespace

Drupal\file\Upload

Code

public function saveFileUploads(array $element, FormStateInterface $formState, ?int $delta = NULL, FileExists $fileExists = FileExists::Rename) : array|FileInterface|null|false {
  // Get all errors set before calling this method. This will also clear them
  // from the messenger service.
  $errorsBefore = $this->messenger
    ->deleteByType(MessengerInterface::TYPE_ERROR);
  $uploadLocation = $element['#upload_location'] ?? 'temporary://';
  $uploadName = implode('_', $element['#parents']);
  $uploadValidators = $element['#upload_validators'] ?? [];
  if ($uploadLocation === FALSE) {
    $uploadLocation = 'temporary://';
  }
  $result = $this->formFileUploader
    ->saveFormUploadedFiles($uploadName, $uploadValidators, $uploadLocation, $delta, $fileExists);
  // Get new errors that are generated while trying to save the upload. This
  // will also clear them from the messenger service.
  $errorsNew = $this->messenger
    ->deleteByType(MessengerInterface::TYPE_ERROR);
  if (!empty($errorsNew)) {
    if (count($errorsNew) > 1) {
      // Render multiple errors into a single message.
      // This is needed because only one error per element is supported.
      $render_array = [
        'error' => [
          '#markup' => $this->t('One or more files could not be uploaded.'),
        ],
        'item_list' => [
          '#theme' => 'item_list',
          '#items' => $errorsNew,
        ],
      ];
      $errorMessage = $this->renderer
        ->renderInIsolation($render_array);
    }
    else {
      $errorMessage = reset($errorsNew);
    }
    $formState->setError($element, $errorMessage);
  }
  // Ensure that errors set prior to calling this method are still shown to
  // the user.
  if (!empty($errorsBefore)) {
    foreach ($errorsBefore as $error) {
      $this->messenger
        ->addError($error);
    }
  }
  return $result;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.