FileManagementDeleteFileConfirmForm.php in File Management 8
File
src/Form/FileManagementDeleteFileConfirmForm.php
View source
<?php
namespace Drupal\file_management\Form;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
class FileManagementDeleteFileConfirmForm extends ConfirmFormBase {
protected $file;
protected $id;
public function getFormId() {
return 'file_management_delete';
}
public function getQuestion() {
return t('Do you want to delete the file "%label" ?', [
'%label' => $this->file
->label(),
]);
}
public function getCancelUrl() {
$route_overview = 'file_management_view.overview';
$route_provider = \Drupal::service('router.route_provider');
if (count($route_provider
->getRoutesByNames([
$route_overview,
])) === 1) {
return new Url($route_overview);
}
return Url::fromRoute('view.files.page_1');
}
public function getDescription() {
return t('This could break some pages and media entities if they use this file.');
}
public function getConfirmText() {
return t('Yes.');
}
public function getCancelText() {
return t('No, go back.');
}
public function buildForm(array $form, FormStateInterface $form_state, $file = NULL) {
$this->file = $file;
$this->id = $file
->id();
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
if (file_exists($this->file
->getFileUri())) {
$this->file
->delete();
\Drupal::messenger()
->addMessage(t('File "%label" has been deleted.', [
'%label' => $this->file
->label(),
]), 'status');
}
else {
\Drupal::messenger()
->addMessage(t('File "%label" could not be deleted.', [
'%label' => $this->file
->label(),
]), 'error');
}
}
}