You are here

public function FileTestForm::submitForm in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/file/tests/file_test/src/Form/FileTestForm.php \Drupal\file_test\Form\FileTestForm::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

core/modules/file/tests/file_test/src/Form/FileTestForm.php, line 81
Contains \Drupal\file_test\Form\FileTestForm.

Class

FileTestForm
File test form class.

Namespace

Drupal\file_test\Form

Code

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

  // Process the upload and perform validation. Note: we're using the
  // form value for the $replace parameter.
  if (!$form_state
    ->isValueEmpty('file_subdir')) {
    $destination = 'temporary://' . $form_state
      ->getValue('file_subdir');
    file_prepare_directory($destination, FILE_CREATE_DIRECTORY);
  }
  else {
    $destination = FALSE;
  }

  // Setup validators.
  $validators = array();
  if ($form_state
    ->getValue('is_image_file')) {
    $validators['file_validate_is_image'] = array();
  }
  if ($form_state
    ->getValue('allow_all_extensions')) {
    $validators['file_validate_extensions'] = array();
  }
  elseif (!$form_state
    ->isValueEmpty('extensions')) {
    $validators['file_validate_extensions'] = array(
      $form_state
        ->getValue('extensions'),
    );
  }

  // The test for drupal_move_uploaded_file() triggering a warning is
  // unavoidable. We're interested in what happens afterwards in
  // file_save_upload().
  if (\Drupal::state()
    ->get('file_test.disable_error_collection')) {
    define('SIMPLETEST_COLLECT_ERRORS', FALSE);
  }
  $file = file_save_upload('file_test_upload', $validators, $destination, 0, $form_state
    ->getValue('file_test_replace'));
  if ($file) {
    $form_state
      ->setValue('file_test_upload', $file);
    drupal_set_message(t('File @filepath was uploaded.', array(
      '@filepath' => $file
        ->getFileUri(),
    )));
    drupal_set_message(t('File name is @filename.', array(
      '@filename' => $file
        ->getFilename(),
    )));
    drupal_set_message(t('File MIME type is @mimetype.', array(
      '@mimetype' => $file
        ->getMimeType(),
    )));
    drupal_set_message(t('You WIN!'));
  }
  elseif ($file === FALSE) {
    drupal_set_message(t('Epic upload FAIL!'), 'error');
  }
}