You are here

public function PluploadTestForm::submitForm in Plupload integration 2.0.x

Same name and namespace in other branches
  1. 8 plupload_test/src/PluploadTestForm.php \Drupal\plupload_test\PluploadTestForm::submitForm()

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

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

Overrides FormInterface::submitForm

File

plupload_test/src/PluploadTestForm.php, line 56

Class

PluploadTestForm
Plupload test form class.

Namespace

Drupal\plupload_test

Code

public function submitForm(array &$form, FormStateInterface $form_state) {

  // Create target directory if necessary.
  $destination = \Drupal::config('system.file')
    ->get('default_scheme') . '://plupload-test';
  \Drupal::service('file_system')
    ->prepareDirectory($destination, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
  $saved_files = [];
  foreach ($form_state
    ->getValue('plupload') as $uploaded_file) {
    $file_uri = $this
      ->loadStreamWrapper()
      ->normalizeUri($destination . '/' . $uploaded_file['name']);

    // Move file without creating a new 'file' entity.
    \Drupal::service('file_system')
      ->move($uploaded_file['tmppath'], $file_uri);

    // @todo: When https://www.drupal.org/node/2245927 is resolved,
    // use a helper to save file to file_managed table
    $saved_files[] = $file_uri;
  }
  if (!empty($saved_files)) {
    \Drupal::messenger()
      ->addStatus('Files uploaded correctly: ' . implode(', ', $saved_files) . '.');
  }
}