You are here

public function ContentImportForm::submitForm in Content Synchronization 8

Same name and namespace in other branches
  1. 8.2 src/Form/ContentImportForm.php \Drupal\content_sync\Form\ContentImportForm::submitForm()
  2. 3.0.x src/Form/ContentImportForm.php \Drupal\content_sync\Form\ContentImportForm::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

src/Form/ContentImportForm.php, line 63

Class

ContentImportForm
Defines the content import form.

Namespace

Drupal\content_sync\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  if ($path = $form_state
    ->getValue('import_tarball')) {
    $directory = content_sync_get_content_directory('sync');
    emptyDirectory($directory);
    try {
      $archiver = new ArchiveTar($path, 'gz');
      $files = [];
      foreach ($archiver
        ->listContent() as $file) {
        $files[] = $file['filename'];
      }
      $archiver
        ->extractList($files, $directory);
      drupal_set_message($this
        ->t('Your content files were successfully uploaded'));
      $this
        ->logger('content_sync')
        ->notice('Your content files were successfully uploaded', [
        'link' => 'Import Archive',
      ]);
      $form_state
        ->setRedirect('content.sync');
    } catch (\Exception $e) {
      drupal_set_message($this
        ->t('Could not extract the contents of the tar file. The error message is <em>@message</em>', [
        '@message' => $e
          ->getMessage(),
      ]), 'error');
      $this
        ->logger('content_sync')
        ->error('Could not extract the contents of the tar file. The error message is <em>@message</em>', [
        '@message' => $e
          ->getMessage(),
        'link' => 'Import Archive',
      ]);
    }
    drupal_flush_all_caches();
    unlink($path);
  }
}