FlagResetForm.php in Flag 8.4
File
src/Form/FlagResetForm.php
View source
<?php
namespace Drupal\flag\Form;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\flag\FlagInterface;
use Drupal\flag\FlagServiceInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class FlagResetForm extends ConfirmFormBase {
protected $flagService;
protected $flag;
public function __construct(FlagServiceInterface $flag_service) {
$this->flagService = $flag_service;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('flag'));
}
public function buildForm(array $form, FormStateInterface $form_state, FlagInterface $flag = NULL) {
$this->flag = $flag;
return parent::buildForm($form, $form_state);
}
public function getFormId() {
return 'flag_reset_confirm_form';
}
public function getQuestion() {
return $this
->t('Are you sure you want to reset the Flag %label?', [
'%label' => $this->flag
->label(),
]);
}
public function getDescription() {
return $this
->t('All flaggings created with Flag %label will be deleted.', [
'%label' => $this->flag
->label(),
]);
}
public function getConfirmText() {
return $this
->t('Reset');
}
public function getCancelUrl() {
return $this->flag
->toUrl('collection');
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->flagService
->unflagAllByFlag($this->flag);
$this
->messenger()
->addMessage($this
->t('Flag %label was reset.', [
'%label' => $this->flag
->label(),
]));
$form_state
->setRedirectUrl($this
->getCancelUrl());
}
}