You are here

public function FileUploadForm::submitForm in Ubercart 8.4

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

uc_file/src/Form/FileUploadForm.php, line 168

Class

FileUploadForm
Performs a file upload action.

Namespace

Drupal\uc_file\Form

Code

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

  // Build the destination location. We start with the base directory,
  // then add any directory which was explicitly selected.
  $dir = $this
    ->config('uc_file.settings')
    ->get('base_dir') . '/' . $form_state
    ->getValue('upload_dir');
  if (is_dir($dir)) {

    // Retrieve our uploaded files.
    $file_objects = $form_state
      ->get('temp_files');
    foreach ($file_objects as $file_object) {

      // Copy the file to its final location.
      if (copy($file_object
        ->getFileUri(), $dir . '/' . $file_object
        ->getFilename())) {

        // Check if any hook_uc_file_action('upload', $args) are implemented.
        foreach ($this->moduleHandler
          ->getImplementations('uc_file_action') as $module) {
          $name = $module . '_uc_file_action';
          $name('upload', [
            'file_object' => $file_object,
            'form_id' => $form_id,
            'form_state' => $form_state,
          ]);
        }

        // Update the file list.
        uc_file_refresh();
        $this
          ->messenger()
          ->addMessage($this
          ->t('The file %file has been uploaded to %dir', [
          '%file' => $file_object
            ->getFilename(),
          '%dir' => $dir,
        ]));
      }
      else {
        $this
          ->messenger()
          ->addError($this
          ->t('An error occurred while copying the file to %dir', [
          '%dir' => $dir,
        ]));
      }
    }
  }
  else {
    $this
      ->messenger()
      ->addError($this
      ->t('Can not move file to %dir', [
      '%dir' => $dir,
    ]));
  }
  $form_state
    ->setRedirectUrl($this
    ->getCancelUrl());
}