You are here

public function EventTypeRuleDeleteAll::submitForm in RNG - Events and Registrations 3.x

Same name and namespace in other branches
  1. 8.2 src/Form/EventTypeRuleDeleteAll.php \Drupal\rng\Form\EventTypeRuleDeleteAll::submitForm()
  2. 8 src/Form/EventTypeRuleDeleteAll.php \Drupal\rng\Form\EventTypeRuleDeleteAll::submitForm()

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormInterface::submitForm

File

src/Form/EventTypeRuleDeleteAll.php, line 110

Class

EventTypeRuleDeleteAll
Form controller to delete all custom rules for an event type.

Namespace

Drupal\rng\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {

  /** @var \Drupal\rng\Entity\RuleInterface[] $rules */
  $rules = $this->ruleStorage
    ->loadByProperties([
    'event__target_type' => $this->eventType
      ->getEventEntityTypeId(),
  ]);

  // There is no bundle field on rules. Load all rules one-by-one and find
  // the bundle for each event.
  $count = 0;
  foreach ($rules as $rule) {
    $event = $rule
      ->getEvent();

    // If event no longer exists then delete the rules while we're here.
    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());
}