Skip to content

Commit a495dc9

Browse files
authored
dpi#166 add event type message template (#2)
* Add per event-type message templates / defaults #1 * Added basic UI to set default messages. * Deleted test file. * Add per event-type message templates / defaults dpi#166
1 parent 3b78226 commit a495dc9

13 files changed

+521
-4
lines changed

config/schema/rng.data_types.schema.yml

+16
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,19 @@ rng_event_type_people_type:
2626
entity_form_mode:
2727
type: string
2828
label: 'Form display mode used when the entity is created inline.'
29+
30+
rng_event_type_default_message:
31+
type: mapping
32+
mapping:
33+
trigger:
34+
type: string
35+
label: 'Trigger'
36+
status:
37+
type: boolean
38+
label: 'Whether this entity bundle can be created inline.'
39+
subject:
40+
type: string
41+
label: 'Subject of the message.'
42+
body:
43+
type: string
44+
label: 'Message body.'

config/schema/rng.schema.yml

+5
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ rng.event_type.*.*:
6363
label: 'Types of people types allowed to be associated with this event type.'
6464
sequence:
6565
type: rng_event_type_people_type
66+
default_messages:
67+
type: sequence
68+
label: 'Default messages for this event type.'
69+
sequence:
70+
type: rng_event_type_default_message
6671

6772
condition.plugin.rng_user_role:
6873
type: condition.plugin.user_role

rng.links.action.yml

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ rng.local_actions:
22
class: \Drupal\Core\Menu\LocalActionDefault
33
deriver: \Drupal\rng\Plugin\Derivative\LocalActions
44

5+
rng.default_messages.add_form:
6+
route_name: entity.event_type.default_messages.add
7+
title: 'Add message'
8+
appears_on:
9+
- entity.event_type.default_messages
10+
511
rng.registration_type.add:
612
route_name: entity.registration_type.add
713
title: 'Add registration type'
@@ -18,4 +24,4 @@ rng.event_type.add:
1824
route_name: entity.event_type.add
1925
title: 'Add event type'
2026
appears_on:
21-
- rng.event_type.overview
27+
- rng.event_type.overview

rng.links.task.yml

+6-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ entity.event_type.access_defaults:
2424
base_route: entity.event_type.edit_form
2525
title: 'Access Defaults'
2626

27+
entity.event_type.default_messages:
28+
route_name: entity.event_type.default_messages
29+
base_route: entity.event_type.edit_form
30+
title: 'Default messages'
31+
2732
# Registration type
2833
entity.registration_type.edit_form:
2934
title: 'Edit'
@@ -91,4 +96,4 @@ entity.rng_rule_component.edit:
9196
rng.config.settings:
9297
route_name: rng.config.settings
9398
title: 'Settings'
94-
base_route: rng.config.settings
99+
base_route: rng.config.settings

rng.module

+7
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ function rng_entity_operation(EntityInterface $entity) {
6262
]),
6363
'weight' => 20,
6464
);
65+
$operations['default_messages'] = array(
66+
'title' => t('Default messages'),
67+
'url' => Url::fromRoute('entity.event_type.default_messages', [
68+
'event_type' => $entity->id(),
69+
]),
70+
'weight' => 25,
71+
);
6572
}
6673

6774
return $operations;

rng.routing.yml

+20
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,26 @@ entity.event_type.access_defaults:
159159
requirements:
160160
_permission: 'administer event types'
161161

162+
entity.event_type.default_messages:
163+
path: '/admin/structure/rng/event_types/manage/{event_type}/messages'
164+
defaults:
165+
_entity_form: 'event_type.event_default_messages'
166+
_title: 'Event type Default messages'
167+
requirements:
168+
_permission: 'administer event types'
169+
170+
entity.event_type.default_messages.add:
171+
path: '/admin/structure/rng/event_types/manage/{event_type}/messages/add'
172+
defaults:
173+
_form: '\Drupal\rng\Form\EventTypeDefaultMessagesAddForm'
174+
_title: 'Add message'
175+
options:
176+
parameters:
177+
event_type:
178+
type: entity:event_type
179+
requirements:
180+
_permission: 'administer event types'
181+
162182
entity.event_type.field_mapping:
163183
path: '/admin/structure/rng/event_types/manage/{event_type}/field_mapping'
164184
defaults:

src/Entity/EventType.php

+23
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
* "edit" = "Drupal\rng\Form\EventTypeForm",
2525
* "delete" = "Drupal\rng\Form\EventTypeDeleteForm",
2626
* "event_access_defaults" = "Drupal\rng\Form\EventTypeAccessDefaultsForm",
27+
* "event_default_messages" = "Drupal\rng\Form\EventTypeDefaultMessagesListForm",
2728
* "field_mapping" = "Drupal\rng\Form\EventTypeFieldMappingForm",
2829
* }
2930
* },
@@ -106,6 +107,13 @@ class EventType extends ConfigEntityBase implements EventTypeInterface {
106107
*/
107108
protected $people_types = [];
108109

110+
/**
111+
* Default messages configured for this event type.
112+
*
113+
* @var array
114+
*/
115+
protected $default_messages = [];
116+
109117
/**
110118
* Fields to add to event bundles.
111119
*
@@ -189,6 +197,21 @@ function getDefaultRegistrantType() {
189197
return $this->default_registrant;
190198
}
191199

200+
/**
201+
* {@inheritdoc}
202+
*/
203+
function getDefaultMessages() {
204+
return $this->default_messages;
205+
}
206+
207+
/**
208+
* {@inheritdoc}
209+
*/
210+
function setDefaultMessages($messages) {
211+
$this->default_messages = $messages;
212+
return $this;
213+
}
214+
192215
/**
193216
* {@inheritdoc}
194217
*/

src/EventMeta.php

+81-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
use Drupal\Core\Entity\EntityInterface;
1111
use Drupal\rng\Entity\Rule;
1212
use Drupal\rng\Entity\RuleComponent;
13+
use Drupal\courier\Entity\TemplateCollection;
14+
use Drupal\courier\Service\CourierManagerInterface;
15+
use Drupal\Core\Action\ActionManager;
1316

1417
/**
1518
* Meta event wrapper for RNG.
@@ -65,6 +68,20 @@ class EventMeta implements EventMetaInterface {
6568
*/
6669
protected $eventManager;
6770

71+
/**
72+
* The courier manager.
73+
*
74+
* @var \Drupal\courier\Service\CourierManagerInterface
75+
*/
76+
protected $courier_manager;
77+
78+
/**
79+
* The action manager.
80+
*
81+
* @var \Drupal\Core\Action\ActionManager
82+
*/
83+
protected $action_manager;
84+
6885
/**
6986
* Constructs a new EventMeta object.
7087
*
@@ -80,16 +97,22 @@ class EventMeta implements EventMetaInterface {
8097
* The RNG configuration service.
8198
* @param \Drupal\rng\EventManagerInterface $event_manager
8299
* The RNG event manager.
100+
* @param \Drupal\courier\Service\CourierManagerInterface $courier_manager
101+
* The courier manager.
102+
* @param \Drupal\Core\Action\ActionManager $action_manager
103+
* The action manager.
83104
* @param \Drupal\Core\Entity\EntityInterface $entity
84105
* The event entity.
85106
*/
86-
public function __construct(EntityManagerInterface $entity_manager, ConfigFactoryInterface $config_factory, SelectionPluginManagerInterface $selection_plugin_manager, IdentityChannelManagerInterface $identity_channel_manager, RngConfigurationInterface $rng_configuration, EventManagerInterface $event_manager, EntityInterface $entity) {
107+
public function __construct(EntityManagerInterface $entity_manager, ConfigFactoryInterface $config_factory, SelectionPluginManagerInterface $selection_plugin_manager, IdentityChannelManagerInterface $identity_channel_manager, RngConfigurationInterface $rng_configuration, EventManagerInterface $event_manager, CourierManagerInterface $courier_manager, ActionManager $action_manager, EntityInterface $entity) {
87108
$this->entityManager = $entity_manager;
88109
$this->configFactory = $config_factory;
89110
$this->selectionPluginManager = $selection_plugin_manager;
90111
$this->identityChannelManager = $identity_channel_manager;
91112
$this->rngConfiguration = $rng_configuration;
92113
$this->eventManager = $event_manager;
114+
$this->courier_manager = $courier_manager;
115+
$this->action_manager = $action_manager;
93116
$this->entity = $entity;
94117
}
95118

@@ -104,6 +127,8 @@ public static function createInstance(ContainerInterface $container, EntityInter
104127
$container->get('plugin.manager.identity_channel'),
105128
$container->get('rng.configuration'),
106129
$container->get('rng.event_manager'),
130+
$container->get('courier.manager'),
131+
$container->get('plugin.manager.action'),
107132
$entity
108133
);
109134
}
@@ -608,4 +633,59 @@ protected function entityTypeHasBundles($entity_type_id) {
608633
return ($entity_type->getBundleEntityType() !== NULL);
609634
}
610635

636+
/**
637+
* {@inheritdoc}
638+
*/
639+
function createDefaultEventMessages() {
640+
// Get Default messages for this Event type.
641+
$default_messages = $this->getEventType()->getDefaultMessages();
642+
if ($default_messages) {
643+
foreach ($default_messages as $default_message) {
644+
// Create Event Messages from Default Messages.
645+
$template_collection = TemplateCollection::create();
646+
$template_collection->save();
647+
$this->courier_manager->addTemplates($template_collection);
648+
$template_collection->setOwner($this->getEvent());
649+
$template_collection->save();
650+
651+
$templates = $template_collection->getTemplates();
652+
/** @var \Drupal\courier\EmailInterface $courier_email */
653+
$courier_email = $templates[0];
654+
$courier_email->setSubject($default_message['subject']);
655+
$courier_email->setBody($default_message['body']);
656+
$courier_email->save();
657+
658+
$rule = Rule::create([
659+
'event' => ['entity' => $this->getEvent()],
660+
'trigger_id' => $default_message['trigger'],
661+
]);
662+
$rule->setIsActive($default_message['status']);
663+
664+
$actionPlugin = $this->action_manager->createInstance('rng_courier_message');
665+
$configuration = $actionPlugin->getConfiguration();
666+
$configuration['template_collection'] = $template_collection->id();
667+
$action = RuleComponent::create([])
668+
->setPluginId($actionPlugin->getPluginId())
669+
->setConfiguration($configuration)
670+
->setType('action');
671+
$rule->addComponent($action);
672+
$rule->save();
673+
674+
// Handle custom date trigger.
675+
if ($default_message['trigger'] == 'rng:custom:date') {
676+
$rule_component = RuleComponent::create()
677+
->setRule($rule)
678+
->setType('condition')
679+
->setPluginId('rng_rule_scheduler');
680+
$rule_component->save();
681+
// Save the ID into config.
682+
$rule_component->setConfiguration([
683+
'rng_rule_component' => $rule_component->id(),
684+
]);
685+
$rule_component->save();
686+
}
687+
}
688+
}
689+
}
690+
611691
}

src/EventMetaInterface.php

+5
Original file line numberDiff line numberDiff line change
@@ -366,4 +366,9 @@ public function identitiesCanRegister($entity_type, array $entity_ids);
366366
*/
367367
function addDefaultAccess();
368368

369+
/**
370+
* Create messages for Event from Default messages for this Event Type.
371+
*/
372+
function createDefaultEventMessages();
373+
369374
}

src/EventTypeInterface.php

+15
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,21 @@ function setAllowCustomRules($allow);
8484
*/
8585
function getDefaultRegistrantType();
8686

87+
/**
88+
* Default messages configured for this event type.
89+
*
90+
* @return array
91+
*/
92+
function getDefaultMessages();
93+
94+
/**
95+
* Set default messages for this event type.
96+
*
97+
* @param array $messages
98+
* Default messages array.
99+
*/
100+
function setDefaultMessages($messages);
101+
87102
/**
88103
* Whether a identity type can be created.
89104
*

0 commit comments

Comments
 (0)