You are here

public function PhotosImageEditForm::submitForm in Album Photos 8.4

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/PhotosImageEditForm.php, line 260

Class

PhotosImageEditForm
Defines a form to edit images.

Namespace

Drupal\photos\Form

Code

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

  // Process image cropping data.
  $form_state_values = $form_state
    ->getValues();
  $fid = $form_state_values['fid'];
  $old_pid = $form_state_values['oldpid'];
  $pid = $form_state_values['pid'];
  $uid = $form_state_values['uid'];

  // Save other image data.
  if (!empty($form_state_values['del'])) {
    if ($form_state
      ->getValue('cover_fid') == $fid) {
      $this->connection
        ->update('photos_album')
        ->fields([
        'fid' => 0,
      ])
        ->condition('pid', $form_state
        ->getValue('oldpid'))
        ->execute();
    }
    $image = new PhotosImage($fid);
    $msg = $image
      ->delete($form_state_values['filepath']);
  }
  else {
    $wid = is_numeric($form_state_values['wid']) ? $form_state_values['wid'] : 0;
    $this->connection
      ->update('photos_image')
      ->fields([
      'pid' => $form_state_values['pid'],
      'des' => $form_state_values['des'],
      'wid' => $wid,
    ])
      ->condition('fid', $fid)
      ->execute();
    if ($form_state_values['title'] != $form_state_values['oldtitle']) {
      $this->connection
        ->update('photos_image')
        ->fields([
        'title' => $form_state_values['title'],
      ])
        ->condition('fid', $fid)
        ->execute();
    }
  }

  // Clear image page cache.
  Cache::invalidateTags([
    'photos:image:' . $fid,
  ]);
  if ($nid = $form_state
    ->getValue('nid')) {

    // Clear album page and node cache.
    Cache::invalidateTags([
      'photos:album:' . $nid,
      'node:' . $nid,
    ]);
  }
  if (isset($pid) && $pid) {
    $pid;

    // Update album count.
    PhotosAlbum::setCount('node_album', $pid);

    // Clear album page and node cache.
    Cache::invalidateTags([
      'photos:album:' . $pid,
      'node:' . $pid,
    ]);
    PhotosAlbum::setCount('user_image', $uid);
    if ($old_pid && $old_pid != $pid) {

      // Update old album count.
      PhotosAlbum::setCount('node_album', $old_pid);

      // Clear old album page and node cache.
      Cache::invalidateTags([
        'photos:album:' . $old_pid,
        'node:' . $old_pid,
      ]);
    }
  }

  // Image deleted or moved.
  if (isset($msg)) {
    $pid = $form_state
      ->getValue('oldpid');
    \Drupal::messenger()
      ->addMessage($this
      ->t('Image deleted.'));

    // Redirect to album page.
    $nid = $form_state
      ->getValue('nid');
    $url = Url::fromUri('base:photos/album/' . $nid);
    $form_state
      ->setRedirectUrl($url);
  }

  // @todo redirect to image page?
  // @todo redirect to destination.
  if (empty($form_state_values['del'])) {
    \Drupal::messenger()
      ->addMessage($this
      ->t('Changes saved.'));
  }
}