You are here

public function MigrateSourceUiForm::validateForm in Migrate source UI 8

Form validation 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 FormBase::validateForm

File

src/Form/MigrateSourceUiForm.php, line 124

Class

MigrateSourceUiForm
Contribute form.

Namespace

Drupal\migrate_source_ui\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  parent::validateForm($form, $form_state);
  $migration_id = $form_state
    ->getValue('migrations');
  $definition = $this->pluginManagerMigration
    ->getDefinition($migration_id);
  $migrationInstance = $this->pluginManagerMigration
    ->createStubMigration($definition);
  $extension = $this
    ->getFileExtensionSupported($migrationInstance);
  $validators = [
    'file_validate_extensions' => [
      $extension,
    ],
  ];

  // Check to see if a specific file temp directory is configured. If not,
  // default the value to FALSE, which will instruct file_save_upload() to
  // use Drupal's temporary files scheme.
  $file_destination = $this->config
    ->get('file_temp_directory');
  if (is_null($file_destination)) {
    $file_destination = FALSE;
  }
  $directory = $this->fileSystem
    ->realpath($file_destination);
  $this->fileSystem
    ->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY);
  $file = file_save_upload('source_file', $validators, $file_destination, 0, FileSystemInterface::EXISTS_REPLACE);
  if (isset($file)) {

    // File upload was attempted.
    if ($file) {
      $form_state
        ->setValue('file_path', $file
        ->getFileUri());
    }
    else {
      $form_state
        ->setErrorByName('source_file', $this
        ->t('The file could not be uploaded.'));
    }
  }
  else {
    $form_state
      ->setErrorByName('source_file', $this
      ->t('You have to upload a source file.'));
  }
}