You are here

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

Class

PhotosImageDeleteForm
Defines a confirmation form for deleting images.

Namespace

Drupal\photos\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $fid = $this->id;
  $pid = $this->connection
    ->query("SELECT pid FROM {photos_image} WHERE fid = :fid", [
    ':fid' => $fid,
  ])
    ->fetchField();

  // Delete image.
  $image = new PhotosImage($fid);
  $v = $image
    ->delete(NULL, TRUE);
  if ($v) {
    \Drupal::messenger()
      ->addMessage(t('Image deleted.'));

    // Invalidate cache tags.
    Cache::invalidateTags([
      'node:' . $pid,
      'photos:album:' . $pid,
      'photos:image:' . $fid,
    ]);

    // @todo redirect to album.
    $url = Url::fromUri('base:photos/album/' . $pid);
    $form_state
      ->setRedirectUrl($url);
  }
  else {
    \Drupal::messenger()
      ->addError(t('Delete failed.'));

    // Redirect to cancel URL.
    $form_state
      ->setRedirectUrl($this
      ->getCancelUrl());
  }
}