MediaWatermarkDeleteForm.php in Media watermark 8
File
src/Form/MediaWatermarkDeleteForm.php
View source
<?php
namespace Drupal\media_watermark\Form;
use Drupal\Core\Entity\EntityConfirmFormBase;
use Drupal\Core\Url;
use Drupal\Core\Form\FormStateInterface;
class MediaWatermarkDeleteForm extends EntityConfirmFormBase {
public function getQuestion() {
return $this
->t('Are you sure you want to delete %name?', [
'%name' => $this->entity
->label(),
]);
}
public function getCancelUrl() {
return new Url('media_watermark.list');
}
public function getConfirmText() {
return $this
->t('Delete');
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$file_storage = $this->entityTypeManager
->getStorage('file');
$watermark = $this->entity;
$file_ids_from_entity = $watermark
->getFid();
$file_ids = is_array($file_ids_from_entity) ? $file_ids_from_entity : [
$file_ids_from_entity,
];
$files = $file_storage
->loadMultiple($file_ids);
$file_names = [];
foreach ($files as $file) {
$file_names[] = $file
->getFilename();
}
if ($files) {
$file_storage
->delete($files);
}
$watermark
->delete();
if ($file_names) {
$message = $this
->t('Watermark "%label" and attached images ( %file_names ) has been deleted.', [
'%label' => $watermark
->label(),
'%file_names' => implode(', ', $file_names),
]);
}
else {
$message = $this
->t('Watermark "%label" has been deleted.', [
'%label' => $watermark
->label(),
]);
}
$this
->messenger()
->addMessage($message);
$form_state
->setRedirectUrl($this
->getCancelUrl());
}
}