class EventTypeForm in RNG - Events and Registrations 8.2
Same name and namespace in other branches
- 8 src/Form/EventTypeForm.php \Drupal\rng\Form\EventTypeForm
- 3.x src/Form/EventTypeForm.php \Drupal\rng\Form\EventTypeForm
Form controller for event config entities.
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\EventTypeForm
- class \Drupal\Core\Entity\EntityForm implements EntityFormInterface
Expanded class hierarchy of EventTypeForm
2 files declare their use of EventTypeForm
- RngEventAccessWebTest.php in tests/
src/ Functional/ RngEventAccessWebTest.php - RngSiteTestBase.php in src/
Tests/ RngSiteTestBase.php
File
- src/
Form/ EventTypeForm.php, line 21
Namespace
Drupal\rng\FormView source
class EventTypeForm extends EntityForm {
/**
* The entity manager.
*
* @var \Drupal\Core\Entity\EntityManagerInterface
*/
protected $entityManager;
/**
* The module handler service.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* The entity display repository.
*
* @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface
*/
protected $entityDisplayRepository;
/**
* The RNG configuration service.
*
* @var \Drupal\rng\RngConfigurationInterface
*/
protected $rngConfiguration;
/**
* The RNG event manager.
*
* @var \Drupal\rng\EventManagerInterface
*/
protected $eventManager;
/**
* Constructs a EventTypeForm object.
*
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity manager.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler service.
* @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entity_display_repository
* The entity display repository.
* @param \Drupal\rng\RngConfigurationInterface $rng_configuration
* The RNG configuration service.
* @param \Drupal\rng\EventManagerInterface $event_manager
* The RNG event manager.
*/
public function __construct(EntityManagerInterface $entity_manager, ModuleHandlerInterface $module_handler, EntityDisplayRepositoryInterface $entity_display_repository, RngConfigurationInterface $rng_configuration, EventManagerInterface $event_manager) {
$this->entityManager = $entity_manager;
$this->moduleHandler = $module_handler;
$this->entityDisplayRepository = $entity_display_repository;
$this->rngConfiguration = $rng_configuration;
$this->eventManager = $event_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('entity.manager'), $container
->get('module_handler'), $container
->get('entity_display.repository'), $container
->get('rng.configuration'), $container
->get('rng.event_manager'));
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
/** @var \Drupal\rng\Entity\EventTypeInterface $event_type */
$event_type = $this->entity;
if (!$event_type
->isNew()) {
$form['#title'] = $this
->t('Edit event type %label configuration', [
'%label' => $event_type
->label(),
]);
}
if ($event_type
->isNew()) {
$bundle_options = [];
// Generate a list of fieldable bundles which are not events.
foreach ($this->entityManager
->getDefinitions() as $entity_type) {
if ($entity_type
->isSubclassOf('\\Drupal\\Core\\Entity\\ContentEntityInterface')) {
foreach ($this->entityManager
->getBundleInfo($entity_type
->id()) as $bundle => $bundle_info) {
if (!$this->eventManager
->eventType($entity_type
->id(), $bundle)) {
$bundle_options[(string) $entity_type
->getLabel()][$entity_type
->id() . '.' . $bundle] = $bundle_info['label'];
}
}
}
}
if ($this->moduleHandler
->moduleExists('node')) {
$form['#attached']['library'][] = 'rng/rng.admin';
$form['entity_type'] = [
'#type' => 'radios',
'#options' => NULL,
'#title' => $this
->t('Event entity type'),
'#required' => TRUE,
// Kills \Drupal\Core\Render\Element\Radios::processRadios.
'#process' => [],
];
$form['entity_type']['node']['radio'] = [
'#type' => 'radio',
'#title' => $this
->t('Create a new content type'),
'#description' => $this
->t('Create a content type to use as an event type.'),
'#return_value' => "node",
'#parents' => [
'entity_type',
],
'#default_value' => 'node',
];
$form['entity_type']['existing']['radio'] = [
'#type' => 'radio',
'#title' => $this
->t('Use existing bundle'),
'#description' => $this
->t('Use an existing entity/bundle combination.'),
'#return_value' => "existing",
'#parents' => [
'entity_type',
],
'#default_value' => '',
];
$form['entity_type']['existing']['container'] = [
'#type' => 'container',
'#attributes' => [
'class' => [
'rng-radio-indent',
],
],
];
}
$form['entity_type']['existing']['container']['bundle'] = [
'#type' => 'select',
'#title' => $this
->t('Bundle'),
'#options' => $bundle_options,
'#default_value' => $event_type
->id(),
'#disabled' => !$event_type
->isNew(),
'#empty_option' => $bundle_options ? NULL : t('No Bundles Available'),
];
}
$form['settings'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Settings'),
];
// Mirror permission.
$form['access']['mirror_update'] = [
'#group' => 'settings',
'#type' => 'checkbox',
'#title' => t('Mirror manage registrations with update permission'),
'#description' => t('Allow users to <strong>manage registrations</strong> if they have <strong>update</strong> permission on an event entity.'),
'#default_value' => (bool) ($event_type
->getEventManageOperation() !== NULL ? $event_type
->getEventManageOperation() : TRUE),
];
// Allow Anonymous Registrants?
$form['allow_anon_registrants'] = [
'#group' => 'settings',
'#type' => 'checkbox',
'#title' => t('Allow anonymous registrants without saving to other identities?'),
'#description' => t('Allow editing registrant data as text fields on a registration. Add fields to the registrant type for information you would like to collect.'),
'#default_value' => (bool) $event_type
->getAllowAnonRegistrants(),
];
// Auto Sync Registrants
$form['auto_sync_registrants'] = [
'#group' => 'settings',
'#type' => 'checkbox',
'#title' => t('Sync matching field data between registrant and registrant identity'),
'#description' => t('If there are empty fields on either the registrant or an identity, copy data from the other when a registrant is updated. This can streamline views of attendee data.'),
'#default_value' => (bool) $event_type
->getAutoSyncRegistrants(),
];
// Auto Attach User Identities
$form['auto_attach_users'] = [
'#group' => 'settings',
'#type' => 'checkbox',
'#title' => t('Automatically add user identities to anonymous registrants if email matches'),
'#description' => t('If an email field in a registrant matches a user account, automatically add the user account as the identity.'),
'#default_value' => (bool) $event_type
->getAutoAttachUsers(),
'#states' => [
'invisible' => [
':input[name="allow_anon_registrants"]' => [
'checked' => FALSE,
],
],
],
];
// Registrant email field
$form['registrant_email_field'] = [
'#group' => 'settings',
'#type' => 'textfield',
'#title' => t('Registrant email field'),
'#description' => t('Machine name of a field on the registrant to use when looking up a user account.'),
'#default_value' => $event_type
->getRegistrantEmailField(),
'#states' => [
'invisible' => [
':input[name="auto_attach_users"]' => [
'checked' => FALSE,
],
],
],
];
// Event date fields
$form['event_date_field_start'] = [
'#group' => 'settings',
'#type' => 'textfield',
'#title' => t('Event Start Date field'),
'#description' => t('Machine name of a field on the event to use as the event start date.'),
'#default_value' => $event_type
->getEventStartDateField(),
];
$form['event_date_field_end'] = [
'#group' => 'settings',
'#type' => 'textfield',
'#title' => t('Event End Date field'),
'#description' => t('Machine name of a field on the event to use as the event end date. Will be combined into a "friendly" date string on registrations.'),
'#default_value' => $event_type
->getEventEndDateField(),
];
$registrant_types = [];
foreach (RegistrantType::loadMultiple() as $registrant_type) {
/** @var \Drupal\rng\Entity\RegistrantTypeInterface $registrant_type */
$registrant_types[$registrant_type
->id()] = $registrant_type
->label();
}
$form['registrants'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Registrants'),
'#tree' => TRUE,
];
// Default registrant type.
$form['registrants']['registrant_type'] = [
'#type' => 'select',
'#title' => $this
->t('Default registrant type'),
'#description' => $this
->t('Registrant type used for new registrants associated with this event type.'),
'#required' => TRUE,
'#options' => $registrant_types,
'#default_value' => $event_type
->getDefaultRegistrantType(),
];
$form['registrants']['registrants'] = [
'#type' => 'table',
'#header' => [
[
'data' => $this
->t('Person type'),
],
[
'data' => $this
->t('Permit inline creation of entities'),
'class' => [
'checkbox',
],
],
[
'data' => $this
->t('Form display mode'),
],
[
'data' => $this
->t('Permit referencing existing entities'),
'class' => [
'checkbox',
],
],
],
'#empty' => $this
->t('There are no people types available.'),
];
foreach ($this->rngConfiguration
->getIdentityTypes() as $entity_type_id) {
$entity_type = $this->entityTypeManager
->getDefinition($entity_type_id);
$bundles = $this->entityManager
->getBundleInfo($entity_type_id);
foreach ($bundles as $bundle => $info) {
$t_args = [
'@bundle' => $info['label'],
'@entity_type' => $entity_type
->getLabel(),
];
$row = [];
$row['people_type']['#markup'] = $this
->t('@bundle (@entity_type)', $t_args);
$row['create'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Permit inline creation of @bundle entities', $t_args),
'#title_display' => 'invisible',
'#default_value' => $event_type
->canIdentityTypeCreate($entity_type_id, $bundle),
'#wrapper_attributes' => [
'class' => [
'checkbox',
],
],
];
$row['entity_form_mode'] = [
'#type' => 'select',
'#title' => $this
->t('Form display mode used when the entity is created inline.', $t_args),
'#title_display' => 'invisible',
'#default_value' => $event_type
->getIdentityTypeEntityFormMode($entity_type_id, $bundle),
'#options' => $this->entityDisplayRepository
->getFormModeOptionsByBundle($entity_type_id, $bundle),
];
$row['existing'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Permit referencing existing @bundle entities', $t_args),
'#title_display' => 'invisible',
'#default_value' => $event_type
->canIdentityTypeReference($entity_type_id, $bundle),
'#wrapper_attributes' => [
'class' => [
'checkbox',
],
],
];
$row_key = "{$entity_type_id}:{$bundle}";
$form['registrants']['registrants'][$row_key] = $row;
}
}
// Check if user people type is enabled, then apply some special handling.
if (isset($form['registrants']['registrants']['user:user'])) {
// Blacklist user creation. It does not work because it is special.
$form['registrants']['registrants']['user:user']['create']['#access'] = FALSE;
// Auto check existing references for users.
if ($event_type
->isNew()) {
$form['registrants']['registrants']['user:user']['existing']['#default_value'] = TRUE;
}
}
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$form_state
->cleanValues();
$this->entity = $this
->buildEntity($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
/** @var \Drupal\rng\Entity\EventTypeInterface $event_type */
$event_type = $this
->getEntity();
if ($event_type
->isNew()) {
if ($this->moduleHandler
->moduleExists('node') && $form_state
->getValue('entity_type') == 'node') {
$node_type = $this
->createContentType('Event');
$t_args = [
'%label' => $node_type
->label(),
':url' => $node_type
->toUrl()
->toString(),
];
drupal_set_message(t('The content type <a href=":url">%label</a> has been added.', $t_args));
$event_type
->setEventEntityTypeId($node_type
->getEntityType()
->getBundleOf());
$event_type
->setEventBundle($node_type
->id());
}
else {
$bundle = explode('.', $form_state
->getValue('bundle'));
$event_type
->setEventEntityTypeId($bundle[0]);
$event_type
->setEventBundle($bundle[1]);
}
}
foreach ($form_state
->getValue([
'registrants',
'registrants',
]) as $row_key => $row) {
list($entity_type, $bundle) = explode(':', $row_key);
$event_type
->setIdentityTypeCreate($entity_type, $bundle, !empty($row['create']));
$event_type
->setIdentityTypeReference($entity_type, $bundle, !empty($row['existing']));
$event_type
->setIdentityTypeEntityFormMode($entity_type, $bundle, $row['entity_form_mode']);
}
$event_type
->setDefaultRegistrantType($form_state
->getValue([
'registrants',
'registrant_type',
]));
// Set to the access operation for event.
$op = $form_state
->getValue('mirror_update') ? 'update' : '';
$event_type
->setEventManageOperation($op)
->setAllowAnonRegistrants($form_state
->getValue('allow_anon_registrants'))
->setAutoSyncRegistrants($form_state
->getValue('auto_sync_registrants'))
->setAutoAttachUsers($form_state
->getValue('auto_attach_users'))
->setRegistrantEmailField($form_state
->getValue('registrant_email_field'))
->setEventStartDateField($form_state
->getValue('event_date_field_start'))
->setEventEndDateField($form_state
->getValue('event_date_field_end'));
$status = $event_type
->save();
// Create default rules.
if ($status == SAVED_NEW) {
$this
->createDefaultRules($event_type
->getEventEntityTypeId(), $event_type
->getEventBundle());
}
$message = $status == SAVED_UPDATED ? '%label event type updated.' : '%label event type added.';
$url = $event_type
->urlInfo();
$t_args = [
'%label' => $event_type
->id(),
'link' => $this
->l(t('Edit'), $url),
];
drupal_set_message($this
->t($message, $t_args));
$this
->logger('rng')
->notice($message, $t_args);
}
/**
* Creates a content type.
*
* Attempts to create a content type with ID $prefix, $prefix_1, $prefix_2...
*
* @param string $prefix
* A string prefix for the node type ID.
*
* @return \Drupal\node\NodeTypeInterface
* A node type entity.
*/
protected function createContentType($prefix) {
// Generate a unique ID.
$i = 0;
$separator = '_';
$id = $prefix;
while (NodeType::load(Unicode::strtolower($id))) {
$i++;
$id = $prefix . $separator . $i;
}
$node_type = NodeType::create([
'type' => Unicode::strtolower($id),
'name' => $id,
]);
$node_type
->save();
return $node_type;
}
/**
* Create default rules for an event type.
*
* @param $entity_type_id
* An entity type ID.
* @param $bundle
* A bundle ID.
*/
public static function createDefaultRules($entity_type_id, $bundle) {
// User Role.
/** @var \Drupal\rng\Entity\EventTypeRuleInterface $rule */
$rule = EventTypeRule::create([
'trigger' => 'rng_event.register',
'entity_type' => $entity_type_id,
'bundle' => $bundle,
'machine_name' => 'user_role',
]);
$rule
->setCondition('role', [
'id' => 'rng_user_role',
'roles' => [],
]);
$rule
->setAction('registration_operations', [
'id' => 'registration_operations',
'configuration' => [
'operations' => [
'create' => TRUE,
],
],
]);
$rule
->save();
// Registrant.
$rule = EventTypeRule::create([
'trigger' => 'rng_event.register',
'entity_type' => $entity_type_id,
'bundle' => $bundle,
'machine_name' => 'registrant',
]);
$rule
->setCondition('identity', [
'id' => 'rng_registration_identity',
]);
$rule
->setAction('registration_operations', [
'id' => 'registration_operations',
'configuration' => [
'operations' => [
'view' => TRUE,
'update' => TRUE,
],
],
]);
$rule
->save();
// Event managers.
$rule = EventTypeRule::create([
'trigger' => 'rng_event.register',
'entity_type' => $entity_type_id,
'bundle' => $bundle,
'machine_name' => 'event_manager',
]);
$rule
->setCondition('operation', [
'id' => 'rng_event_operation',
'operations' => [
'manage event' => TRUE,
],
]);
$rule
->setAction('registration_operations', [
'id' => 'registration_operations',
'configuration' => [
'operations' => [
'create' => TRUE,
'view' => TRUE,
'update' => TRUE,
'delete' => TRUE,
],
],
]);
$rule
->save();
}
}
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 name of the current operation. | |
EntityForm:: |
private | property | The entity manager. | |
EntityForm:: |
protected | function | Returns an array of supported actions for the current entity form. | 29 |
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 | ||
EntityForm:: |
public | function | ||
EventTypeForm:: |
protected | property | The entity display repository. | |
EventTypeForm:: |
protected | property | The entity manager. | |
EventTypeForm:: |
protected | property | The RNG event manager. | |
EventTypeForm:: |
protected | property |
The module handler service. Overrides EntityForm:: |
|
EventTypeForm:: |
protected | property | The RNG configuration service. | |
EventTypeForm:: |
public | function |
Form constructor. Overrides EntityForm:: |
|
EventTypeForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
EventTypeForm:: |
protected | function | Creates a content type. | |
EventTypeForm:: |
public static | function | Create default rules for an event type. | |
EventTypeForm:: |
public | function |
Form submission handler for the 'save' action. Overrides EntityForm:: |
|
EventTypeForm:: |
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 EntityForm:: |
|
EventTypeForm:: |
public | function | Constructs a EventTypeForm 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 | property | The redirect destination service. | 1 |
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. |