You are here

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

Class

FileAddArchiveForm
Form controller for archive type forms.

Namespace

Drupal\file_entity\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  if ($archive = File::load($form_state
    ->getValue('upload')[0])) {
    if ($archiver = $this->archiverManager
      ->getInstance([
      'filepath' => $this->fileSystem
        ->realpath($archive
        ->getFileUri()),
    ])) {
      $extract_dir = $this
        ->config('system.file')
        ->get('default_scheme') . '://' . pathinfo($archive
        ->getFilename(), PATHINFO_FILENAME);
      $extract_dir = $this->fileSystem
        ->getDestinationFilename($extract_dir, FileSystemInterface::EXISTS_RENAME);
      if (!$this->fileSystem
        ->prepareDirectory($extract_dir, FileSystemInterface::MODIFY_PERMISSIONS | FileSystemInterface::CREATE_DIRECTORY)) {
        throw new \Exception(t('Unable to prepare, the directory %dir for extraction.', array(
          '%dir' => $extract_dir,
        )));
      }
      $archiver
        ->extract($extract_dir);
      $pattern = '/' . $form_state
        ->getValue('pattern') . '/';
      if ($files = $this->fileSystem
        ->scanDirectory($extract_dir, $pattern)) {
        foreach ($files as $file) {
          $file = File::create([
            'uri' => $file->uri,
            'filename' => $file->filename,
            'status' => FILE_STATUS_PERMANENT,
          ]);
          $file
            ->save();
        }
        $all_files = $this->fileSystem
          ->scanDirectory($extract_dir, '/.*/');

        // Get all files that don't match the pattern so we can remove them.
        $remainig_files = array_diff_key($all_files, $files);
        foreach ($remainig_files as $file) {
          $this->fileSystem
            ->unlink($file->uri);
        }
      }
      $this->messenger
        ->addMessage($this
        ->t('Extracted %file and added @count new files.', array(
        '%file' => $archive
          ->getFilename(),
        '@count' => count($files),
      )));
      if ($form_state
        ->getValue('remove_archive')) {
        $this->messenger
          ->addMessage($this
          ->t('Archive %name was removed from the system.', array(
          '%name' => $archive
            ->getFilename(),
        )));
        $archive
          ->delete();
      }
      else {
        $archive
          ->setPermanent();
        $archive
          ->save();
      }
    }
    else {
      $form_state
        ->setErrorByName('', $this
        ->t('Cannot extract %file, not a valid archive.', array(
        '%file' => $archive
          ->getFileUri(),
      )));
    }
  }
  $this
    ->redirect('entity.file.collection')
    ->send();
}