CustomFilterRulesDeleteForm.php in Custom filter 2.0.x
File
src/Form/CustomFilterRulesDeleteForm.php
View source
<?php
namespace Drupal\customfilter\Form;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Url;
use Drupal\Core\Form\FormStateInterface;
use Drupal\customfilter\Entity\CustomFilter;
class CustomFilterRulesDeleteForm extends ConfirmFormBase {
protected $customfilter;
protected $rule;
public function buildForm(array $form, FormStateInterface $form_state, CustomFilter $customfilter = NULL, $rule_id = NULL) {
$this->customfilter = $customfilter;
$this->rule = $this->customfilter
->getRule($rule_id);
return parent::buildForm($form, $form_state);
}
public function getQuestion() {
return $this
->t('Are you sure you want to delete %name?', [
'%name' => $this->rule['name'],
]);
}
public function getCancelUrl() {
return new Url('customfilter.rules.list', [
'customfilter' => $this->customfilter
->id(),
]);
}
public function getConfirmText() {
return $this
->t('Delete');
}
public function getDescription() {
return $this
->t('This will delete the rule %name and all sub-rules. This
action cannot be undone.', [
'%name' => $this->rule['name'],
]);
}
public function getFormId() {
return 'customfilter_rules_delete_form';
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->customfilter
->deleteRule($this->rule['rid']);
$this->customfilter
->save();
$this
->messenger()
->addStatus($this
->t('The rule %label has been deleted.', [
'%label' => $this->rule['name'],
]));
$form_state
->setRedirect('customfilter.rules.list', [
'customfilter' => $this->customfilter
->id(),
]);
}
}