EventTypeRuleDeleteAll.php in RNG - Events and Registrations 3.x
File
src/Form/EventTypeRuleDeleteAll.php
View source
<?php
namespace Drupal\rng\Form;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\rng\EventManagerInterface;
use Drupal\rng\Entity\EventTypeInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Url;
use Drupal\Core\Form\FormStateInterface;
class EventTypeRuleDeleteAll extends ConfirmFormBase {
protected $eventType;
protected $ruleStorage;
protected $eventManager;
public function __construct(EntityTypeManagerInterface $entity_type_manager, EventManagerInterface $event_manager) {
$this->ruleStorage = $entity_type_manager
->getStorage('rng_rule');
$this->eventManager = $event_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'), $container
->get('rng.event_manager'));
}
public function getFormId() {
return 'rng_event_type_rule_delete_all';
}
public function getQuestion() {
return $this
->t('Are you sure you want to delete custom access rules for all events?');
}
public function getDescription() {
return $this
->t('All custom rules for events will be deleted. All events will use event type defaults.');
}
public function getConfirmText() {
return $this
->t('Delete all existing access rules');
}
public function getCancelUrl() {
return Url::fromRoute('entity.rng_event_type.access_defaults', [
'event_type' => $this->eventType
->id(),
]);
}
public function buildForm(array $form, FormStateInterface $form_state, EventTypeInterface $event_type = NULL) {
$this->eventType = $event_type;
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$rules = $this->ruleStorage
->loadByProperties([
'event__target_type' => $this->eventType
->getEventEntityTypeId(),
]);
$count = 0;
foreach ($rules as $rule) {
$event = $rule
->getEvent();
if (!$event || $event
->bundle() == $this->eventType
->getEventBundle()) {
$rule
->delete();
$count++;
}
}
$this
->messenger()
->addMessage($this
->formatPlural($count, '@count custom access rule deleted.', '@count custom access rules deleted.'));
$this->eventManager
->invalidateEventType($this->eventType);
$form_state
->setRedirectUrl($this
->getCancelUrl());
}
}