10
10
use Drupal \Core \Entity \EntityInterface ;
11
11
use Drupal \rng \Entity \Rule ;
12
12
use Drupal \rng \Entity \RuleComponent ;
13
+ use Drupal \courier \Entity \TemplateCollection ;
14
+ use Drupal \courier \Service \CourierManagerInterface ;
15
+ use Drupal \Core \Action \ActionManager ;
13
16
14
17
/**
15
18
* Meta event wrapper for RNG.
@@ -65,6 +68,20 @@ class EventMeta implements EventMetaInterface {
65
68
*/
66
69
protected $ eventManager ;
67
70
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
+
68
85
/**
69
86
* Constructs a new EventMeta object.
70
87
*
@@ -80,16 +97,22 @@ class EventMeta implements EventMetaInterface {
80
97
* The RNG configuration service.
81
98
* @param \Drupal\rng\EventManagerInterface $event_manager
82
99
* 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.
83
104
* @param \Drupal\Core\Entity\EntityInterface $entity
84
105
* The event entity.
85
106
*/
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 ) {
87
108
$ this ->entityManager = $ entity_manager ;
88
109
$ this ->configFactory = $ config_factory ;
89
110
$ this ->selectionPluginManager = $ selection_plugin_manager ;
90
111
$ this ->identityChannelManager = $ identity_channel_manager ;
91
112
$ this ->rngConfiguration = $ rng_configuration ;
92
113
$ this ->eventManager = $ event_manager ;
114
+ $ this ->courier_manager = $ courier_manager ;
115
+ $ this ->action_manager = $ action_manager ;
93
116
$ this ->entity = $ entity ;
94
117
}
95
118
@@ -104,6 +127,8 @@ public static function createInstance(ContainerInterface $container, EntityInter
104
127
$ container ->get ('plugin.manager.identity_channel ' ),
105
128
$ container ->get ('rng.configuration ' ),
106
129
$ container ->get ('rng.event_manager ' ),
130
+ $ container ->get ('courier.manager ' ),
131
+ $ container ->get ('plugin.manager.action ' ),
107
132
$ entity
108
133
);
109
134
}
@@ -608,4 +633,59 @@ protected function entityTypeHasBundles($entity_type_id) {
608
633
return ($ entity_type ->getBundleEntityType () !== NULL );
609
634
}
610
635
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
+
611
691
}
0 commit comments