Skip to content

Commit c1de5c5

Browse files
committed
Add per event-type message templates / defaults dpi#166
1 parent c3f3ccc commit c1de5c5

File tree

3 files changed

+97
-1
lines changed

3 files changed

+97
-1
lines changed

src/EventMeta.php

+87
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use Drupal\Core\Entity\EntityInterface;
1111
use Drupal\rng\Entity\Rule;
1212
use Drupal\rng\Entity\RuleComponent;
13+
use Drupal\rng\Entity\RuleSchedule;
14+
use Drupal\courier\Entity\TemplateCollection;
1315

1416
/**
1517
* Meta event wrapper for RNG.
@@ -608,4 +610,89 @@ protected function entityTypeHasBundles($entity_type_id) {
608610
return ($entity_type->getBundleEntityType() !== NULL);
609611
}
610612

613+
/**
614+
* {@inheritdoc}
615+
*/
616+
function createDefaultEventMessages() {
617+
// Get Default messages for this Event type.
618+
$default_messages = $this->getEventType()->getDefaultMessages();
619+
// TODO: remove these test values.
620+
$default_messages = array(
621+
array(
622+
'trigger' => 'entity:registration:new',
623+
'options' => array(),
624+
'status' => 1,
625+
'subject' => 'Test subj',
626+
'body' => 'Test text',
627+
),
628+
array(
629+
'trigger' => 'entity:registration:new',
630+
'options' => array(),
631+
'status' => 0,
632+
'subject' => 'Test subj DIS',
633+
'body' => 'Test text DIS',
634+
),
635+
array(
636+
'trigger' => 'rng:custom:date',
637+
'options' => array('date' => 1568283861),
638+
'status' => 1,
639+
'subject' => 'Test subj Date',
640+
'body' => 'Test text Date',
641+
),
642+
);
643+
644+
if ($default_messages) {
645+
foreach ($default_messages as $default_message) {
646+
// Create Event Messages from Default Messages.
647+
/** @var \Drupal\courier\Service\CourierManagerInterface $courier_manager */
648+
$courier_manager = \Drupal::service('courier.manager');
649+
/** @var \Drupal\Core\Action\ActionManager $action_manager */
650+
$action_manager = \Drupal::service('plugin.manager.action');
651+
652+
$template_collection = TemplateCollection::create();
653+
$template_collection->save();
654+
$courier_manager->addTemplates($template_collection);
655+
$template_collection->setOwner($this->getEvent());
656+
$template_collection->save();
657+
658+
$templates = $template_collection->getTemplates();
659+
/** @var \Drupal\courier\EmailInterface $courier_email */
660+
$courier_email = $templates[0];
661+
$courier_email->setSubject($default_message['subject']);
662+
$courier_email->setBody($default_message['body']);
663+
$courier_email->save();
664+
665+
$rule = Rule::create([
666+
'event' => ['entity' => $this->getEvent()],
667+
'trigger_id' => $default_message['trigger'],
668+
]);
669+
$rule->setIsActive($default_message['status']);
670+
671+
$actionPlugin = $action_manager->createInstance('rng_courier_message');
672+
$configuration = $actionPlugin->getConfiguration();
673+
$configuration['template_collection'] = $template_collection->id();
674+
$action = RuleComponent::create([])
675+
->setPluginId($actionPlugin->getPluginId())
676+
->setConfiguration($configuration)
677+
->setType('action');
678+
$rule->addComponent($action);
679+
$rule->save();
680+
681+
// Handle custom date trigger.
682+
if ($default_message['trigger'] == 'rng:custom:date') {
683+
$rule_component = RuleComponent::create()
684+
->setRule($rule)
685+
->setType('condition')
686+
->setPluginId('rng_rule_scheduler');
687+
$rule_component->save();
688+
// Save the ID into config.
689+
$rule_component->setConfiguration([
690+
'rng_rule_component' => $rule_component->id(),
691+
]);
692+
$rule_component->save();
693+
}
694+
}
695+
}
696+
}
697+
611698
}

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/RngEntityModel.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ public function hook_entity_postsave(EntityInterface $entity, $update = TRUE) {
106106
->setEntityId($entity->id());
107107
$this->operationRecords[] = $operation_record;
108108
}
109+
if ($this->eventManager->isEvent($entity) && $update == FALSE) {
110+
$event = $this->eventManager->getMeta($entity);
111+
$event->createDefaultEventMessages();
112+
}
109113
}
110114

111115
/**
@@ -229,4 +233,4 @@ protected function deleteRngRuleComponent(RuleComponentInterface $entity) {
229233
}
230234
}
231235

232-
}
236+
}

0 commit comments

Comments
 (0)