FacDeleteFilesForm.php in Fast Autocomplete 8
File
src/Form/FacDeleteFilesForm.php
View source
<?php
namespace Drupal\fac\Form;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
class FacDeleteFilesForm extends ConfirmFormBase {
protected $facConfigStorage;
protected $facConfig;
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->facConfigStorage = $entity_type_manager
->getStorage('fac_config');
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'));
}
public function getFormId() {
return 'fac_delete_files_form';
}
public function getQuestion() {
return $this
->t('Are you sure you want to delete all Fast Autocomplete json files for %label?', [
'%label' => $this->facConfig
->label(),
]);
}
public function getCancelUrl() {
return new Url('entity.fac_config.collection');
}
public function getConfirmText() {
return $this
->t('Delete');
}
public function getCancelText() {
return $this
->t('Cancel');
}
public function buildForm(array $form, FormStateInterface $form_state, $fac_config_id = '') {
$this->facConfig = $this->facConfigStorage
->load($fac_config_id);
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->facConfig
->deleteFiles();
$this
->messenger()
->addStatus($this
->t('Fast Autocomplete json files for %label have been deleted.', [
'%label' => $this->facConfig
->label(),
]));
$form_state
->setRedirectUrl($this
->getCancelUrl());
}
}