You are here

public static function PlUploadFile::validatePlUploadFile in Plupload integration 8

Same name and namespace in other branches
  1. 2.0.x src/Element/PlUploadFile.php \Drupal\plupload\Element\PlUploadFile::validatePlUploadFile()

Render API callback: Validates the managed_file element.

Note: based on plupload_element_validate().

File

src/Element/PlUploadFile.php, line 262

Class

PlUploadFile
Provides a PLUpload widget for uploading and saving files.

Namespace

Drupal\plupload\Element

Code

public static function validatePlUploadFile(&$element, FormStateInterface $form_state, &$complete_form) {
  foreach ($element['#value'] as $file_info) {

    // Here we create a $file object for a file that doesn't exist yet,
    // because saving the file to its destination is done in a submit handler.
    // Using tmp path will give validators access to the actual file on disk
    // and filesize information. We manually modify filename and mime to allow
    // extension checks.
    $destination = \Drupal::config('system.file')
      ->get('default_scheme') . '://' . $file_info['name'];
    $file_uri = \Drupal::service('stream_wrapper_manager')
      ->normalizeUri($destination);
    $file = File::create([
      'uri' => $file_info['tmppath'],
      'uid' => \Drupal::currentUser()
        ->id(),
      'status' => FILE_STATUS_PERMANENT,
      'filename' => \Drupal::service('file_system')
        ->basename($destination),
      'filemime' => \Drupal::service('file.mime_type.guesser')
        ->guess($destination),
    ]);
    foreach (file_validate($file, $element['#upload_validators']) as $error_message) {
      $message = t('The specified file %name could not be uploaded.', [
        '%name' => $file
          ->getFilename(),
      ]);
      $concatenated_message = $message . ' ' . $error_message;
      $form_state
        ->setError($element, $concatenated_message);
    }
  }
}