PhotosImageDeleteForm.php in Album Photos 8.4
File
src/Form/PhotosImageDeleteForm.php
View source
<?php
namespace Drupal\photos\Form;
use Drupal\Core\Database\Connection;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\Core\Cache\Cache;
use Drupal\photos\PhotosImage;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class PhotosImageDeleteForm extends ConfirmFormBase {
protected $connection;
protected $id;
public function __construct(Connection $connection) {
$this->connection = $connection;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('database'));
}
public function getFormId() {
return 'photos_image_confirm_delete';
}
public function getQuestion() {
return t('Do you want to delete this image?');
}
public function getCancelUrl() {
$url = Url::fromUri('base:photos/image/' . $this->id);
return $url;
}
public function getDescription() {
return t('Only do this if you are sure!');
}
public function getConfirmText() {
return t('Delete it!');
}
public function getCancelText() {
return t('Nevermind');
}
public function buildForm(array $form, FormStateInterface $form_state, $file = '') {
$this->id = $file;
if (!$this->id) {
throw new NotFoundHttpException();
}
return parent::buildForm($form, $form_state);
}
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();
$image = new PhotosImage($fid);
$v = $image
->delete(NULL, TRUE);
if ($v) {
\Drupal::messenger()
->addMessage(t('Image deleted.'));
Cache::invalidateTags([
'node:' . $pid,
'photos:album:' . $pid,
'photos:image:' . $fid,
]);
$url = Url::fromUri('base:photos/album/' . $pid);
$form_state
->setRedirectUrl($url);
}
else {
\Drupal::messenger()
->addError(t('Delete failed.'));
$form_state
->setRedirectUrl($this
->getCancelUrl());
}
}
}