Skip to content
Snippets Groups Projects
Commit f9212a59 authored by Owen Bush's avatar Owen Bush Committed by Owen Bush
Browse files

Issue #3091919 by owenbush, the_glitch: Registrant Settings needs a label field

parent b1a3a7bb
No related branches found
No related tags found
No related merge requests found
Showing
with 174 additions and 36 deletions
show_capacity: true
limit: 10
date_format: 'F jS, Y h:iA'
title: '[registrant:email]'
email_notifications: true
registration_notification_enabled: true
registration_notification_enabled: true
......
......@@ -11,6 +11,9 @@ recurring_events_registration.registrant.config:
date_format:
type: string
label: 'The formatting of dates when displaying registrants'
title:
type: string
label: 'The title of the registrants'
email_notifications:
type: boolean
label: 'Whether to enable email notifications'
......
......@@ -6,6 +6,7 @@
*/
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\recurring_events_registration\Plugin\Field\ComputedRegistrantTitleFieldItemList;
/**
* Install the schema updates for eventseries entities to add registration.
......@@ -31,3 +32,22 @@ function recurring_events_registration_install() {
\Drupal::entityDefinitionUpdateManager()
->installFieldStorageDefinition('event_registration', 'eventseries', 'eventseries', $storage_definition);
}
/**
* Add the computed title field to registrants.
*/
function recurring_events_registration_update_8001() {
$storage_definition = BaseFieldDefinition::create('string')
->setLabel(t('Title'))
->setReadOnly(TRUE)
->setComputed(TRUE)
->setClass(ComputedRegistrantTitleFieldItemList::class);
\Drupal::entityDefinitionUpdateManager()
->installFieldStorageDefinition('title', 'registrant', 'registrant', $storage_definition);
// Configure the registrants title field.
\Drupal::configFactory()->getEditable('recurring_events_registration.registrant.config')
->set('title', '[registrant:email]')
->save(TRUE);
}
services:
recurring_events_registration.creation_service:
class: Drupal\recurring_events_registration\RegistrationCreationService
arguments: ['@string_translation', '@database', '@logger.factory', '@messenger', '@entity_type.manager']
arguments: ['@string_translation', '@database', '@logger.factory', '@messenger', '@entity_type.manager', '@module_handler', '@token']
recurring_events_registration.notification_service:
class: Drupal\recurring_events_registration\NotificationService
arguments: ['@string_translation', '@config.factory', '@logger.factory', '@messenger', '@token', '@module_handler']
arguments: ['@string_translation', '@config.factory', '@logger.factory', '@messenger', '@token', '@module_handler', '@recurring_events_registration.creation_service']
\ No newline at end of file
......@@ -35,6 +35,11 @@ function recurring_events_registration_token_info() {
'needs-data' => 'registrant',
];
$registrant['email'] = [
'name' => t('Registrant Email'),
'description' => t('The email of the registrant.'),
];
$registrant['edit_url'] = [
'name' => t('Edit Registrant URL'),
'description' => t('The URL to edit a registrant.'),
......@@ -85,6 +90,10 @@ function recurring_events_registration_tokens($type, $tokens, array $data, array
$registrant = $data['registrant'];
foreach ($tokens as $name => $original) {
switch ($name) {
case 'email':
$replacements[$original] = $registrant->email->value;
break;
case 'edit_url':
$url = $registrant->toUrl('edit-form')->setAbsolute(TRUE)->toString();
if ($registrant->user_id->target_id === '0') {
......
......@@ -10,6 +10,7 @@ use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\user\UserInterface;
use Drupal\recurring_events\Entity\EventInstance;
use Drupal\recurring_events\Entity\EventSeries;
use Drupal\recurring_events_registration\Plugin\Field\ComputedRegistrantTitleFieldItemList;
/**
* Defines the Registrant entity.
......@@ -42,6 +43,7 @@ use Drupal\recurring_events\Entity\EventSeries;
* "id" = "id",
* "uuid" = "uuid",
* "uid" = "user_id",
* "label" = "title",
* },
* links = {
* "canonical" = "/events/{eventinstance}/registrant/{registrant}",
......@@ -204,6 +206,12 @@ class Registrant extends ContentEntityBase implements RegistrantInterface {
->setLabel(t('Changed'))
->setDescription(t('The time that the entity was last edited.'));
$fields['title'] = BaseFieldDefinition::create('string')
->setLabel(t('Title'))
->setReadOnly(TRUE)
->setComputed(TRUE)
->setClass(ComputedRegistrantTitleFieldItemList::class);
return $fields;
}
......
......@@ -8,6 +8,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Url;
use Drupal\Core\Link;
use Drupal\recurring_events_registration\NotificationService;
use Drupal\recurring_events_registration\RegistrationCreationService;
/**
* Class RegistrantSettingsForm.
......@@ -23,14 +24,24 @@ class RegistrantSettingsForm extends ConfigFormBase {
*/
protected $notificationService;
/**
* The registration creation service.
*
* @var \Drupal\recurring_events_registration\RegistrationCreationService
*/
protected $creationService;
/**
* Constructs a RegistrantSettingsForm object.
*
* @param \Drupal\recurring_events_registration\NotificationService $notification_service
* The registration notification service.
* @param \Drupal\recurring_events_registration\RegistrationCreationService $creation_service
* The registration creation service.
*/
public function __construct(NotificationService $notification_service) {
public function __construct(NotificationService $notification_service, RegistrationCreationService $creation_service) {
$this->notificationService = $notification_service;
$this->creationService = $creation_service;
}
/**
......@@ -38,7 +49,8 @@ class RegistrantSettingsForm extends ConfigFormBase {
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('recurring_events_registration.notification_service')
$container->get('recurring_events_registration.notification_service'),
$container->get('recurring_events_registration.creation_service')
);
}
......@@ -72,6 +84,7 @@ class RegistrantSettingsForm extends ConfigFormBase {
->set('show_capacity', $form_state->getValue('show_capacity'))
->set('limit', $form_state->getValue('limit'))
->set('date_format', $form_state->getValue('date_format'))
->set('title', $form_state->getValue('title'))
->set('email_notifications', $form_state->getValue('email_notifications'));
$notification_types = [];
......@@ -141,6 +154,20 @@ class RegistrantSettingsForm extends ConfigFormBase {
'#default_value' => $config->get('date_format'),
];
$registrant_tokens = $this->creationService->getAvailableTokens(['registrant']);
$form['display']['title'] = [
'#type' => 'textfield',
'#title' => $this->t('Registrant Title'),
'#required' => TRUE,
'#description' => $this->t('Enter the format for the title field', [
'@link' => $php_date_link->toString(),
]),
'#default_value' => $config->get('title'),
];
$form['display']['tokens'] = $registrant_tokens;
$form['notifications'] = [
'#type' => 'details',
'#title' => $this->t('Email Notifications'),
......
......@@ -57,6 +57,13 @@ class NotificationService {
*/
protected $moduleHandler;
/**
* The registration creation service.
*
* @var \Drupal\recurring_events_registration\RegistrationCreationService
*/
protected $creationService;
/**
* The registrant entity.
*
......@@ -121,14 +128,17 @@ class NotificationService {
* The token service.
* @param \Drupal\Core\Extension\ModuleHandler $module_handler
* The module handler service.
* @param \Drupal\recurring_events_registration\RegistrationCreationService $creation_service
* The registration creation service.
*/
public function __construct(TranslationInterface $translation, ConfigFactory $config_factory, LoggerChannelFactoryInterface $logger, Messenger $messenger, Token $token, ModuleHandler $module_handler) {
public function __construct(TranslationInterface $translation, ConfigFactory $config_factory, LoggerChannelFactoryInterface $logger, Messenger $messenger, Token $token, ModuleHandler $module_handler, RegistrationCreationService $creation_service) {
$this->translation = $translation;
$this->configFactory = $config_factory;
$this->loggerFactory = $logger->get('recurring_events_registration');
$this->messenger = $messenger;
$this->token = $token;
$this->moduleHandler = $module_handler;
$this->creationService = $creation_service;
$this->configName = 'recurring_events_registration.registrant.config';
}
......@@ -143,7 +153,7 @@ class NotificationService {
$container->get('messenger'),
$container->get('token'),
$container->get('module_handler'),
$container->get('string_translation')
$container->get('recurring_events_registration.creation_service')
);
}
......@@ -430,34 +440,7 @@ class NotificationService {
'registrant',
];
if ($this->moduleHandler->moduleExists('token')) {
$token_help = [
'#theme' => 'token_tree_link',
'#token_types' => $relevant_tokens,
];
}
else {
$all_tokens = $this->token->getInfo();
$tokens = [];
foreach ($relevant_tokens as $token_prefix) {
if (!empty($all_tokens['tokens'][$token_prefix])) {
foreach ($all_tokens['tokens'][$token_prefix] as $token_key => $value) {
$tokens[] = '[' . $token_prefix . ':' . $token_key . ']';
}
}
}
$token_text = $this->translation->translate('Available tokens are: @tokens', [
'@tokens' => implode(', ', $tokens),
]);
$token_help = [
'#type' => 'markup',
'#markup' => $token_text->render(),
];
}
return $token_help;
return $this->creationService->getAvailableTokens($relevant_tokens);
}
}
<?php
namespace Drupal\recurring_events_registration\Plugin\Field;
use Drupal\Core\Field\FieldItemList;
use Drupal\Core\TypedData\ComputedItemListTrait;
/**
* The ComputedRegistrantTitleFieldItemList class.
*/
final class ComputedRegistrantTitleFieldItemList extends FieldItemList {
use ComputedItemListTrait;
/**
* {@inheritdoc}
*/
protected function computeValue() {
$registrant = $this->getEntity();
$config = \Drupal::config('recurring_events_registration.registrant.config');
$title_config = $config->get('title');
$data = [
'registrant' => $registrant,
];
$title = \Drupal::service('token')->replace($title_config, $data);
$this->list[0] = $this->createItem(0, $title);
}
}
......@@ -11,6 +11,8 @@ use Drupal\recurring_events\Entity\EventSeries;
use Drupal\Core\Messenger\Messenger;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
use Drupal\Core\Entity\EntityTypeManager;
use Drupal\Core\Extension\ModuleHandler;
use Drupal\Core\Utility\Token;
/**
* RegistrationCreationService class.
......@@ -66,6 +68,20 @@ class RegistrationCreationService {
*/
protected $eventSeries;
/**
* The module handler service.
*
* @var \Drupal\Core\Extension\ModuleHandler
*/
protected $moduleHandler;
/**
* The token service.
*
* @var \Drupal\Core\Utility\Token
*/
protected $token;
/**
* Class constructor.
*
......@@ -79,13 +95,19 @@ class RegistrationCreationService {
* The messenger service.
* @param \Drupal\Core\Entity\EntityTypeManager $entity_type_manager
* The entity type manager service.
* @param \Drupal\Core\Extension\ModuleHandler $module_handler
* The module handler service.
* @param \Drupal\Core\Utility\Token $token
* The token service.
*/
public function __construct(TranslationInterface $translation, Connection $database, LoggerChannelFactoryInterface $logger, Messenger $messenger, EntityTypeManager $entity_type_manager) {
public function __construct(TranslationInterface $translation, Connection $database, LoggerChannelFactoryInterface $logger, Messenger $messenger, EntityTypeManager $entity_type_manager, ModuleHandler $module_handler, Token $token) {
$this->translation = $translation;
$this->database = $database;
$this->loggerFactory = $logger->get('recurring_events_registration');
$this->messenger = $messenger;
$this->storage = $entity_type_manager->getStorage('registrant');
$this->moduleHandler = $module_handler;
$this->token = $token;
}
/**
......@@ -97,7 +119,9 @@ class RegistrationCreationService {
$container->get('database'),
$container->get('logger.factory'),
$container->get('messenger'),
$container->get('entity_type.manager')
$container->get('entity_type.manager'),
$container->get('module_handler'),
$container->get('token')
);
}
......@@ -494,4 +518,38 @@ class RegistrationCreationService {
}
}
/**
* Retrieve the tokens available for a registrant.
*/
public function getAvailableTokens($relevant_tokens = ['registrant']) {
if ($this->moduleHandler->moduleExists('token')) {
$token_help = [
'#theme' => 'token_tree_link',
'#token_types' => $relevant_tokens,
];
}
else {
$all_tokens = $this->token->getInfo();
$tokens = [];
foreach ($relevant_tokens as $token_prefix) {
if (!empty($all_tokens['tokens'][$token_prefix])) {
foreach ($all_tokens['tokens'][$token_prefix] as $token_key => $value) {
$tokens[] = '[' . $token_prefix . ':' . $token_key . ']';
}
}
}
$token_text = $this->translation->translate('Available tokens are: @tokens', [
'@tokens' => implode(', ', $tokens),
]);
$token_help = [
'#type' => 'markup',
'#markup' => $token_text->render(),
];
}
return $token_help;
}
}
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