View source
<?php
namespace Drupal\rng\Form;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Routing\RedirectDestinationInterface;
use Drupal\Core\Action\ActionManager;
use Drupal\Core\Condition\ConditionManager;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\rng\EventManagerInterface;
use Drupal\rng\RNGConditionInterface;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Url;
class EventTypeAccessDefaultsForm extends EntityForm {
protected $redirectDestination;
protected $actionManager;
protected $conditionManager;
protected $eventTypeRuleStorage;
protected $eventManager;
protected $rules;
public function __construct(RedirectDestinationInterface $redirect_destination, ActionManager $actionManager, ConditionManager $conditionManager, EntityTypeManagerInterface $entity_type_manager, EventManagerInterface $event_manager) {
$this->redirectDestination = $redirect_destination;
$this->actionManager = $actionManager;
$this->conditionManager = $conditionManager;
$this->eventTypeRuleStorage = $entity_type_manager
->getStorage('rng_event_type_rule');
$this->eventManager = $event_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('redirect.destination'), $container
->get('plugin.manager.action'), $container
->get('plugin.manager.condition'), $container
->get('entity_type.manager'), $container
->get('rng.event_manager'));
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
$form['table'] = [
'#type' => 'table',
'#empty' => $this
->t('No access rules.'),
];
$event_type = $this->entity;
$trigger = 'rng_event.register';
$form['table']['head'] = [
'#attributes' => [],
[
'#wrapper_attributes' => [
'header' => TRUE,
'rowspan' => 2,
],
'#plain_text' => $this
->t('Rule'),
],
[
'#wrapper_attributes' => [
'header' => TRUE,
'rowspan' => 2,
'colspan' => 2,
],
'#plain_text' => $this
->t('Condition'),
],
[
'#wrapper_attributes' => [
'header' => TRUE,
'rowspan' => 2,
'width' => 100,
],
'#plain_text' => $this
->t('Links'),
],
[
'#wrapper_attributes' => [
'header' => TRUE,
'rowspan' => 2,
'colspan' => 2,
],
'#plain_text' => $this
->t('Scope'),
],
[
'#wrapper_attributes' => [
'header' => TRUE,
'rowspan' => 1,
'colspan' => 4,
],
'#plain_text' => $this
->t('Operations'),
],
];
$operations = [
'create' => $this
->t('Create'),
'view' => $this
->t('View'),
'update' => $this
->t('Update'),
'delete' => $this
->t('Delete'),
];
foreach ($operations as $operation) {
$form['table']['operations'][] = [
'#wrapper_attributes' => [
'header' => TRUE,
'class' => [
'checkbox',
],
],
'#plain_text' => $operation,
];
}
$i = 0;
$this->rules = $this->eventTypeRuleStorage
->loadByProperties([
'entity_type' => $event_type
->getEventEntityTypeId(),
'bundle' => $event_type
->getEventBundle(),
'trigger' => $trigger,
]);
foreach ($this->rules as $rule_id => $rule) {
$i++;
$scope_all = FALSE;
$supports_create = 0;
$condition_context = [];
$k = 0;
$row = [];
$row['rule'] = [
'#wrapper_attributes' => [
'header' => FALSE,
],
'#plain_text' => $this
->t('@row.', [
'@row' => $i,
]),
];
foreach ($rule
->getConditions() as $id => $condition) {
$k++;
$row[] = [
'#wrapper_attributes' => [
'header' => TRUE,
],
'#markup' => $this
->t('@condition.', [
'@condition' => $k,
]),
];
$condition = $this->conditionManager
->createInstance($condition['id'], $condition);
$condition_context += array_keys($condition
->getContextDefinitions());
$scope_all = !in_array('registration', $condition_context) || in_array('event', $condition_context);
if ($condition instanceof RNGConditionInterface) {
$supports_create++;
}
$row[] = [
'#markup' => $condition
->summary(),
];
$row[] = [
'#type' => 'operations',
'#links' => [
'edit' => [
'title' => t('Edit'),
'url' => Url::fromRoute('rng.rng_event_type_rule.component.edit', [
'rng_event_type_rule' => $rule
->id(),
'component_type' => 'condition',
'component_id' => $id,
]),
'query' => $this->redirectDestination
->getAsArray(),
],
],
];
if ($scope_all) {
$row[] = [
'#wrapper_attributes' => [
'colspan' => 2,
],
'#plain_text' => $this
->t('Single registration'),
];
}
else {
$row[] = [
'#plain_text' => $this
->t('All registrations.'),
];
$row[] = [
'#wrapper_attributes' => [
'width' => '20%',
],
'#markup' => $this
->t('<strong>Warning:</strong> selecting view, update, or delete grants access to any registration on this event.'),
];
}
if ($k == 1) {
$row = array_merge($row, $this
->actionRows($rule, $operations, $scope_all, $supports_create));
}
$form['table'][] = $row;
$row = [];
}
}
$form['settings'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Settings'),
];
$form['settings']['custom_rules'] = [
'#title' => $this
->t('Allow default rules customization'),
'#description' => $this
->t('Allow event managers to customize event default rules. Changing this setting does not affect existing rules.'),
'#type' => 'checkbox',
'#default_value' => $event_type
->getAllowCustomRules(),
];
return $form;
}
public function actionRows($rule, array $operations, $scope_all, $supports_create) {
$rowspan = count($rule
->getConditions());
if ($action = $rule
->getAction('registration_operations')) {
$row = [];
$config = $action['configuration'];
foreach ($operations as $op => $t) {
$disabled = $op == 'create' && $supports_create != count($rule
->getConditions());
if ($disabled) {
$row[] = [
'#plain_text' => $this
->t('N/A'),
'#wrapper_attributes' => [
'class' => [
'checkbox',
],
],
];
}
else {
$row[] = [
'#type' => 'checkbox',
'#title' => $this
->t('@operation', [
'@operation' => $t,
]),
'#title_display' => 'invisible',
'#default_value' => !empty($config['operations'][$op]),
'#parents' => [
'actions',
'operations',
$rule
->getMachineName(),
$op,
],
'#wrapper_attributes' => [
'class' => [
'checkbox',
],
'rowspan' => $rowspan,
],
];
}
}
return $row;
}
}
public function save(array $form, FormStateInterface $form_state) {
$event_type = $this->entity;
$registration_operations = $form_state
->getValue([
'actions',
'operations',
]);
foreach ($this->rules as $rule) {
$rule
->setAction('registration_operations', [
'id' => 'registration_operations',
'configuration' => [
'operations' => $registration_operations[$rule
->getMachineName()],
],
]);
$rule
->save();
}
$event_type
->setAllowCustomRules($form_state
->getValue('custom_rules'))
->save();
Cache::invalidateTags([
'rendered',
]);
$this
->messenger()
->addMessage($this
->t('Event type access defaults saved.'));
$this->eventManager
->invalidateEventType($event_type);
}
protected function actions(array $form, FormStateInterface $form_state) {
$event_type = $this->entity;
$actions = parent::actions($form, $form_state);
unset($actions['delete']);
$actions['delete-custom-rules'] = [
'#type' => 'link',
'#title' => $this
->t('Delete all custom rules'),
'#attributes' => [
'class' => [
'button',
'button--danger',
],
],
];
$actions['delete-custom-rules']['#url'] = Url::fromRoute('entity.rng_event_type.access_defaults.delete_all', [
'rng_event_type' => $event_type
->id(),
]);
return $actions;
}
}