You are here

public function ActionForm::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/ActionForm.php, line 240

Class

ActionForm
Performs file action (upload, delete, hooked in actions).

Namespace

Drupal\uc_file\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  switch ($form_state
    ->getValue('action')) {
    case 'uc_file_delete':

      // File deletion status.
      $status = TRUE;

      // Delete the selected file(s), with recursion if selected.
      $status = uc_file_remove_by_id($form_state
        ->getValue('file_ids'), !$form_state
        ->isValueEmpty('recurse_directories')) && $status;
      if ($status) {
        $this
          ->messenger()
          ->addMessage($this
          ->t('The selected file(s) have been deleted.'));
      }
      else {
        $this
          ->messenger()
          ->addWarning($this
          ->t('One or more files could not be deleted.'));
      }
      break;
    case 'uc_file_upload':

      // 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 file.
        $file_object = $form_state
          ->get('temp_file');

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

          // Check for hook_uc_file_action('upload', $args) implementations.
          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->filename,
            '%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,
        ]));
      }
      break;
    default:

      // This action isn't handled by us, so check if any
      // hook_uc_file_action('submit', $args) are implemented.
      foreach ($this->moduleHandler
        ->getImplementations('uc_file_action') as $module) {
        $name = $module . '_uc_file_action';
        $name('submit', [
          'form_id' => $form_id,
          'form_state' => $form_state,
        ]);
      }
      break;
  }

  // Return to the original form state.
  $form_state
    ->setRebuild(FALSE);
  $this
    ->redirect('uc_file.downloads');
}