You are here

public function EventMeta::createDefaultEventMessages in RNG - Events and Registrations 8.2

Same name and namespace in other branches
  1. 3.x src/EventMeta.php \Drupal\rng\EventMeta::createDefaultEventMessages()

Create messages for Event from Default messages for this Event Type.

Overrides EventMetaInterface::createDefaultEventMessages

File

src/EventMeta.php, line 655

Class

EventMeta
Meta event wrapper for RNG.

Namespace

Drupal\rng

Code

public function createDefaultEventMessages() {

  // Get Default messages for this Event type.
  $default_messages = $this
    ->getEventType()
    ->getDefaultMessages();
  if ($default_messages) {
    foreach ($default_messages as $default_message) {

      // Create Event Messages from Default Messages.
      $template_collection = TemplateCollection::create();
      $template_collection
        ->save();
      $this->courier_manager
        ->addTemplates($template_collection);
      $template_collection
        ->setOwner($this
        ->getEvent());
      $template_collection
        ->save();
      $templates = $template_collection
        ->getTemplates();

      /** @var \Drupal\courier\EmailInterface $courier_email */
      $courier_email = $templates[0];
      $courier_email
        ->setSubject($default_message['subject']);
      $courier_email
        ->setBody($default_message['body']);
      $courier_email
        ->save();
      $rule = Rule::create([
        'event' => [
          'entity' => $this
            ->getEvent(),
        ],
        'trigger_id' => $default_message['trigger'],
      ]);
      $rule
        ->setIsActive($default_message['status']);
      $actionPlugin = $this->action_manager
        ->createInstance('rng_courier_message');
      $configuration = $actionPlugin
        ->getConfiguration();
      $configuration['template_collection'] = $template_collection
        ->id();
      $action = RuleComponent::create([])
        ->setPluginId($actionPlugin
        ->getPluginId())
        ->setConfiguration($configuration)
        ->setType('action');
      $rule
        ->addComponent($action);
      $rule
        ->save();

      // Handle custom date trigger.
      if ($default_message['trigger'] == 'rng:custom:date') {
        $rule_component = RuleComponent::create()
          ->setRule($rule)
          ->setType('condition')
          ->setPluginId('rng_rule_scheduler');
        $rule_component
          ->save();

        // Save the ID into config.
        $rule_component
          ->setConfiguration([
          'rng_rule_component' => $rule_component
            ->id(),
        ]);
        $rule_component
          ->save();
      }
    }
  }
}