class EventTypeAccessDefaultsForm in RNG - Events and Registrations 8.2
Same name and namespace in other branches
- 8 src/Form/EventTypeAccessDefaultsForm.php \Drupal\rng\Form\EventTypeAccessDefaultsForm
- 3.x src/Form/EventTypeAccessDefaultsForm.php \Drupal\rng\Form\EventTypeAccessDefaultsForm
Form for event type access defaults.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
- class \Drupal\rng\Form\EventTypeAccessDefaultsForm
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
Expanded class hierarchy of EventTypeAccessDefaultsForm
File
- src/
Form/ EventTypeAccessDefaultsForm.php, line 20
Namespace
Drupal\rng\FormView source
class EventTypeAccessDefaultsForm extends EntityForm {
/**
* The redirect destination service.
*
* @var \Drupal\Core\Routing\RedirectDestinationInterface
*/
protected $redirectDestination;
/**
* The action manager service.
*
* @var \Drupal\Core\Action\ActionManager
*/
protected $actionManager;
/**
* The condition manager service.
*
* @var \Drupal\Core\Condition\ConditionManager
*/
protected $conditionManager;
/**
* Event type rule storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $eventTypeRuleStorage;
/**
* The RNG event manager.
*
* @var \Drupal\rng\EventManagerInterface
*/
protected $eventManager;
/**
* Rules for the event type.
*
* @var \Drupal\rng\Entity\EventTypeRuleInterface[]
*/
protected $rules;
/**
* Constructs a EventTypeAccessDefaultsForm object.
*
* @param \Drupal\Core\Routing\RedirectDestinationInterface $redirect_destination
* The redirect destination service.
* @param \Drupal\Core\Action\ActionManager $actionManager
* The action manager.
* @param \Drupal\Core\Condition\ConditionManager $conditionManager
* The condition manager.
* @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(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;
}
/**
* {@inheritdoc}
*/
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'));
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
$form['table'] = [
'#type' => 'table',
'#empty' => $this
->t('No access rules.'),
];
/** @var \Drupal\rng\Entity\EventTypeInterface $event_type */
$event_type = $this->entity;
$trigger = 'rng_event.register';
// Header.
$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'),
],
// Love to have 'Condition' column as colspan=3 and omit this header, but
// theme renders 'operations' widget incorrectly when only one link is
// present.
[
'#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 = [];
// Conditions.
$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(),
],
],
];
// Scope: warn user actions apply to all registrations.
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());
// Actions.
if ($action = $rule
->getAction('registration_operations')) {
$row = [];
// Operations granted.
$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;
}
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
/** @var \Drupal\rng\Entity\EventTypeInterface $event_type */
$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();
// Site cache needs to be cleared after enabling this setting as there are
// issue regarding caching.
// For some reason actions access is not reset if pages are rendered with no
// access/viability.
Cache::invalidateTags([
'rendered',
]);
drupal_set_message($this
->t('Event type access defaults saved.'));
$this->eventManager
->invalidateEventType($event_type);
}
/**
* {@inheritdoc}
*
* Remove delete element since it is confusing on non CRUD forms.
*/
protected function actions(array $form, FormStateInterface $form_state) {
/** @var \Drupal\rng\Entity\EventTypeInterface $event_type */
$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;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
EntityForm:: |
protected | property | The entity being used by this form. | 7 |
EntityForm:: |
protected | property | The entity type manager. | 3 |
EntityForm:: |
protected | property | The module handler service. | |
EntityForm:: |
protected | property | The name of the current operation. | |
EntityForm:: |
private | property | The entity manager. | |
EntityForm:: |
protected | function | Returns the action form element for the current entity form. | |
EntityForm:: |
public | function | Form element #after_build callback: Updates the entity with submitted data. | |
EntityForm:: |
public | function |
Builds an updated entity object based upon the submitted form values. Overrides EntityFormInterface:: |
2 |
EntityForm:: |
protected | function | Copies top-level form values to entity properties | 7 |
EntityForm:: |
public | function | Gets the actual form array to be built. | 30 |
EntityForm:: |
public | function |
Returns a string identifying the base form. Overrides BaseFormIdInterface:: |
5 |
EntityForm:: |
public | function |
Gets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Determines which entity will be used by this form from a RouteMatch object. Overrides EntityFormInterface:: |
1 |
EntityForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
10 |
EntityForm:: |
public | function |
Gets the operation identifying the form. Overrides EntityFormInterface:: |
|
EntityForm:: |
protected | function | Initialize the form state and the entity before the first form build. | 3 |
EntityForm:: |
protected | function | Prepares the entity object before the form is built first. | 3 |
EntityForm:: |
protected | function | Invokes the specified prepare hook variant. | |
EntityForm:: |
public | function | Process callback: assigns weights and hides extra fields. | |
EntityForm:: |
public | function |
Sets the form entity. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the entity manager for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the entity type manager for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the module handler for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
Sets the operation for this form. Overrides EntityFormInterface:: |
|
EntityForm:: |
public | function |
This is the default entity object builder function. It is called before any
other submit handler to build the new entity object to be used by the
following submit handlers. At this point of the form workflow the entity is
validated and the form state… Overrides FormInterface:: |
17 |
EntityForm:: |
public | function | ||
EntityForm:: |
public | function | ||
EventTypeAccessDefaultsForm:: |
protected | property | The action manager service. | |
EventTypeAccessDefaultsForm:: |
protected | property | The condition manager service. | |
EventTypeAccessDefaultsForm:: |
protected | property | The RNG event manager. | |
EventTypeAccessDefaultsForm:: |
protected | property | Event type rule storage. | |
EventTypeAccessDefaultsForm:: |
protected | property |
The redirect destination service. Overrides RedirectDestinationTrait:: |
|
EventTypeAccessDefaultsForm:: |
protected | property | Rules for the event type. | |
EventTypeAccessDefaultsForm:: |
public | function | ||
EventTypeAccessDefaultsForm:: |
protected | function |
Remove delete element since it is confusing on non CRUD forms. Overrides EntityForm:: |
|
EventTypeAccessDefaultsForm:: |
public | function |
Form constructor. Overrides EntityForm:: |
|
EventTypeAccessDefaultsForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
EventTypeAccessDefaultsForm:: |
public | function |
Form submission handler for the 'save' action. Overrides EntityForm:: |
|
EventTypeAccessDefaultsForm:: |
public | function | Constructs a EventTypeAccessDefaultsForm object. | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
62 |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |