You are here

public function FileAddForm::submitForm in File Entity (fieldable files) 8.2

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

src/Form/FileAddForm.php, line 335

Class

FileAddForm
Form controller for file type forms.

Namespace

Drupal\file_entity\Form

Code

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

  // This var is set to TRUE when we are ready to save the file.
  $save = FALSE;
  $trigger = $form_state
    ->getTriggeringElement()['#id'];
  $current_step = $form_state
    ->get('step');

  // Store select values in $form_state.
  foreach (array(
    'type',
    'scheme',
  ) as $key) {
    if ($form_state
      ->hasValue($key)) {
      $form_state
        ->set($key, $form_state
        ->getValue($key));
    }
  }
  $steps_to_check = array(
    2,
    3,
  );
  if ($trigger == 'edit-previous') {

    // If the previous button was hit,
    // the step checking order should be reversed 3, 2.
    $steps_to_check = array_reverse($steps_to_check);
  }

  /* @var \Drupal\file\FileInterface $file */
  if ($form_state
    ->has('file')) {
    $file = $form_state
      ->get('file');
  }
  else {
    $file = File::load($form_state
      ->getValue(array(
      'upload',
      0,
    )));
    $form_state
      ->set('file', $file);
  }
  foreach ($steps_to_check as $step) {

    // Check if we can skip step 2 and 3.
    if ($current_step == $step - 1 && $trigger == 'edit-next' || $current_step == $step + 1 && $trigger == 'edit-previous') {
      if ($step == 2) {

        // Check if we can skip step 2.
        $candidates = $this
          ->getCandidateFileTypes($file);
        if (count($candidates) == 1) {
          $candidates_keys = array_keys($candidates);

          // There is only one possible filetype for this file.
          // Skip the second page.
          $current_step += $trigger == 'edit-previous' ? -1 : 1;
          $form_state
            ->set('type', reset($candidates_keys));
        }
        elseif (\Drupal::config('file_entity.settings')
          ->get('wizard_skip_file_type')) {

          // Do not assign the file a file type.
          $current_step += $trigger == 'edit-previous' ? -1 : 1;
          $form_state
            ->set('type', FILE_TYPE_NONE);
        }
      }
      else {

        // Check if we can skip step 3.
        $schemes = \Drupal::service('stream_wrapper_manager')
          ->getWrappers(StreamWrapperInterface::WRITE_VISIBLE);
        if (!$file
          ->isWritable()) {

          // The file is read-only (remote) and must use its provided scheme.
          $current_step += $trigger == 'edit-previous' ? -1 : 1;
          $form_state
            ->set('scheme', StreamWrapperManager::getScheme($file
            ->getFileUri()));
        }
        elseif (count($schemes) == 1) {

          // There is only one possible stream wrapper for this file.
          // Skip the third page.
          $current_step += $trigger == 'edit-previous' ? -1 : 1;
          $form_state
            ->set('scheme', key($schemes));
        }
        elseif (\Drupal::config('file_entity.settings')
          ->get('wizard_skip_scheme')) {

          // Assign the file the default scheme.
          $current_step += $trigger == 'edit-previous' ? -1 : 1;
          $form_state
            ->set('scheme', $this
            ->config('system.file')
            ->get('default_scheme'));
        }
      }
    }
  }

  // We have the filetype, check if we can skip step 4.
  if ($current_step == 3 && $trigger == 'edit-next') {
    $file
      ->updateBundle($form_state
      ->get('type'));
    $save = TRUE;
    foreach ($file
      ->getFieldDefinitions() as $field_definition) {
      if ($field_definition instanceof FieldConfigInterface) {

        // This filetype does have configurable fields, do not save as we
        // do step 4 first.
        $save = FALSE;
        break;
      }
    }
    if ($this
      ->config('file_entity.settings')
      ->get('wizard_skip_fields', FALSE)) {

      // Save the file with blanks fields.
      $save = TRUE;
    }
  }

  // Form id's can vary depending on how many other forms are displayed, so we
  // need to do string comparissons. e.g edit-submit--2.
  if (strpos($trigger, 'edit-next') !== FALSE) {
    $current_step++;
  }
  elseif (strpos($trigger, 'edit-previous') !== FALSE) {
    $current_step--;
  }
  elseif (strpos($trigger, 'edit-submit') !== FALSE) {
    $save = TRUE;
  }
  $form_state
    ->set('step', $current_step);
  if ($save) {
    if (StreamWrapperManager::getScheme($file
      ->getFileUri()) != $form_state
      ->get('scheme')) {

      // @TODO: Users should not be allowed to create private files without permission ('view private files')
      if ($moved_file = file_move($file, $form_state
        ->get('scheme') . '://' . StreamWrapperManager::getTarget($file
        ->getFileUri()), FileSystemInterface::EXISTS_RENAME)) {

        // Only re-assign the file object if file_move() did not fail.
        $moved_file
          ->setFilename($file
          ->getFilename());
        $file = $moved_file;
      }
    }
    $file->display = TRUE;

    // Change the file from temporary to permanent.
    $file->status = FILE_STATUS_PERMANENT;

    // Save entity
    $file
      ->save();
    $form_state
      ->set('file', $file);
    $this->messenger
      ->addMessage(t('@type %name was uploaded.', array(
      '@type' => $file->type->entity
        ->label(),
      '%name' => $file
        ->getFilename(),
    )));

    // Figure out destination.
    if (\Drupal::currentUser()
      ->hasPermission('administer files')) {
      $form_state
        ->setRedirect('entity.file.collection');
    }
    else {
      $form_state
        ->setRedirectUrl($file
        ->toUrl());
    }
  }
  else {
    $form_state
      ->setRebuild();
  }
}