You are here

public function PhotosDirectoryImportForm::submitForm in Album Photos 8.4

Same name and namespace in other branches
  1. 8.5 src/Form/PhotosDirectoryImportForm.php \Drupal\photos\Form\PhotosDirectoryImportForm::submitForm()
  2. 6.0.x src/Form/PhotosDirectoryImportForm.php \Drupal\photos\Form\PhotosDirectoryImportForm::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/PhotosDirectoryImportForm.php, line 183

Class

PhotosDirectoryImportForm
Defines a form to upload photos to this site.

Namespace

Drupal\photos\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $config = $this
    ->config('photos.settings');
  $user_value = $form_state
    ->getValue('user');
  $copy = $form_state
    ->getValue('copy');
  if ($user_value) {
    $form_state
      ->setRebuild();
  }
  else {

    // @todo check if file is already in use before moving?
    // - If in use copy?
    $album = $form_state
      ->getValue('album');
    $directory = $form_state
      ->getValue('directory');
    $nid = $album;
    $album_uid = $form_state
      ->getValue('uid');

    // If photos_access is enabled check viewid.
    $scheme = 'default';
    $album_viewid = 0;
    if ($this->moduleHandler
      ->moduleExists('photos_access')) {
      $node = $this->entityTypeManager
        ->getStorage('node')
        ->load($nid);
      if (isset($node->privacy) && isset($node->privacy['viewid'])) {
        $album_viewid = $node->privacy['viewid'];
        if ($album_viewid > 0) {

          // Check for private file path.
          if (PrivateStream::basePath()) {
            $scheme = 'private';
          }
          else {

            // Set warning message.
            \Drupal::messenger()
              ->addWarning($this
              ->t('Warning: image
                files can still be accessed by visiting the direct URL. For
                better security, ask your website admin to setup a private
                file path.'));
          }
        }
      }
    }
    $account = $this->entityTypeManager
      ->getStorage('user')
      ->load($album_uid);

    // Check if zip is included.
    $allow_zip = $config
      ->get('photos_upzip') ? '|zip|ZIP' : '';
    $file_extensions = 'png|PNG|jpg|JPG|jpeg|JPEG|gif|GIF' . $allow_zip;
    $files = $this->fileSystem
      ->scanDirectory($directory, '/^.*\\.(' . $file_extensions . ')$/');

    // Prepare batch.
    $batch_args = [
      $files,
      $account,
      $nid,
      $scheme,
      $allow_zip,
      $file_extensions,
      $copy,
    ];
    $batch = [
      'title' => $this
        ->t('Moving images to gallery'),
      'operations' => [
        [
          '\\Drupal\\photos\\PhotosUpload::moveImageFiles',
          $batch_args,
        ],
      ],
      'finished' => '\\Drupal\\photos\\PhotosUpload::finishedMovingImageFiles',
    ];
    batch_set($batch);
  }
}