Skip to content
Snippets Groups Projects
Commit 1c86bd93 authored by dpi's avatar dpi
Browse files

Added ability to delete all custom rules for an event type.

Fixed #72
parent 764fb436
No related branches found
No related tags found
No related merge requests found
......@@ -151,6 +151,18 @@ entity.event_type.access_defaults:
requirements:
_permission: 'administer event types'
entity.event_type.access_defaults.delete_all:
path: '/admin/structure/rng/event_types/manage/{event_type}/access_defaults/delete'
defaults:
_form: '\Drupal\rng\Form\EventTypeRuleDeleteAll'
_title: 'Delete all event custom rules'
requirements:
_permission: 'administer event types'
options:
parameters:
rng_event_type_rule:
type: 'entity:rng_event_type_rule'
rng.event_type.overview:
path: '/admin/structure/rng/event_types'
defaults:
......@@ -161,7 +173,7 @@ rng.event_type.overview:
_permission: 'administer event types'
# Event type rules
rng.rng_event_type_rule:
rng.rng_event_type_rule.component.edit:
path: '/admin/structure/rng/event_type_rule/{rng_event_type_rule}/{component_type}/{component_id}/edit'
defaults:
_form: '\Drupal\rng\Form\EventTypeRuleComponentEdit'
......
......@@ -17,6 +17,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\rng\EventManagerInterface;
use Drupal\rng\RNGConditionInterface;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Url;
/**
* Form for event type access defaults.
......@@ -201,7 +202,7 @@ class EventTypeAccessDefaultsForm extends EntityForm {
'#type' => 'operations',
'#links' => ['edit' => [
'title' => t('Edit'),
'url' => \Drupal\Core\Url::fromRoute('rng.rng_event_type_rule', [
'url' => Url::fromRoute('rng.rng_event_type_rule.component.edit', [
'rng_event_type_rule' => $rule->id(),
'component_type' => 'condition',
'component_id' => $id,
......@@ -321,8 +322,24 @@ class EventTypeAccessDefaultsForm extends EntityForm {
* Remove delete element since it is confusing on non CRUD forms.
*/
protected function actions(array $form, FormStateInterface $form_state) {
/** @var \Drupal\rng\EventTypeInterface $event_type */
$event_type = $this->entity;
$actions = parent::actions($form, $form_state);
unset($actions['delete']);
$actions['delete-custom-rules'] = array(
'#type' => 'link',
'#title' => $this->t('Delete all custom rules'),
'#attributes' => array(
'class' => array('button', 'button--danger'),
),
);
$actions['delete-custom-rules']['#url'] = Url::fromRoute('entity.event_type.access_defaults.delete_all', [
'event_type' => $event_type->id(),
]);
return $actions;
}
......
<?php
/**
* @file
* Contains \Drupal\rng\Form\EventTypeRuleDeleteAll.
*/
namespace Drupal\rng\Form;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\rng\EventManagerInterface;
use Drupal\rng\EventTypeInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Url;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Cache\Cache;
/**
* Form controller to delete all custom rules for an event type.
*/
class EventTypeRuleDeleteAll extends ConfirmFormBase {
/**
* The event type entity.
*
* @var \Drupal\rng\EventTypeInterface
*/
protected $eventType;
/**
* Rule storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $ruleStorage;
/**
* The RNG event manager.
*
* @var \Drupal\rng\EventManagerInterface
*/
protected $eventManager;
/**
* Constructs a EventTypeRuleDeleteAll form.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\rng\EventManagerInterface $event_manager
* The RNG event manager.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, EventManagerInterface $event_manager) {
$this->ruleStorage = $entity_type_manager->getStorage('rng_rule');
$this->eventManager = $event_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity_type.manager'),
$container->get('rng.event_manager')
);
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'rng_event_type_rule_delete_all';
}
/**
* {@inheritdoc}
*/
public function getQuestion() {
return $this->t('Are you sure you want to delete custom access rules for all events?');
}
/**
* {@inheritdoc}
*/
public function getDescription() {
return $this->t('All custom rules for events will be deleted. All events will use event type defaults.');
}
/**
* {@inheritdoc}
*/
public function getConfirmText() {
return $this->t('Delete all existing access rules');
}
/**
* {@inheritdoc}
*/
public function getCancelUrl() {
return Url::fromRoute('entity.event_type.access_defaults', [
'event_type' => $this->eventType->id(),
]);
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, EventTypeInterface $event_type = NULL) {
$this->eventType = $event_type;
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
/** @var \Drupal\rng\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++;
}
}
drupal_set_message($this->formatPlural($count, '@count custom access rule deleted.', '@count custom access rules deleted.'));
$this->eventManager->invalidateEventType($this->eventType);
$form_state->setRedirectUrl($this->getCancelUrl());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment