|
10 | 10 | use Drupal\Core\Entity\EntityInterface;
|
11 | 11 | use Drupal\rng\Entity\Rule;
|
12 | 12 | use Drupal\rng\Entity\RuleComponent;
|
| 13 | +use Drupal\rng\Entity\RuleSchedule; |
| 14 | +use Drupal\courier\Entity\TemplateCollection; |
13 | 15 |
|
14 | 16 | /**
|
15 | 17 | * Meta event wrapper for RNG.
|
@@ -608,4 +610,89 @@ protected function entityTypeHasBundles($entity_type_id) {
|
608 | 610 | return ($entity_type->getBundleEntityType() !== NULL);
|
609 | 611 | }
|
610 | 612 |
|
| 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 | + |
611 | 698 | }
|
0 commit comments