rng icon indicating copy to clipboard operation
rng copied to clipboard

Add ability to create default messages for new events.

Open dpi opened this issue 9 years ago • 4 comments

From #79 / LP

dpi avatar Mar 28 '16 18:03 dpi

Here's what I'm using, not sure if it's helpful. Create a default message on new registrations for nodes of type 'event'.

/**
 * Create a default message for new events.
 *
 * @see \Drupal\rng\Form\MessageActionForm::submitForm()
 * @see \Drupal\courier\Tests\CourierTest::testCourier()
 *
 * Implements hook_ENTITY_TYPE_create().
 */
function mymodule_node_insert(\Drupal\Core\Entity\EntityInterface $node) {
  if ($node->bundle() != 'event') {
    // Bail out if not an event node.
    return;
  }

  // Set up services that we need
  $container = Drupal::getContainer();
  $action_manager = $container->get('plugin.manager.action');
  $entityManager = $container->get('entity.manager');
  /** @var \Drupal\rng\Plugin\Action\CourierTemplateCollection $actionPlugin */
  $actionPlugin = $action_manager->createInstance('rng_courier_message');

  // Create a new template collection by faking a form submission.
  $actionPlugin->submitConfigurationForm($dummy = array(), new \Drupal\Core\Form\FormState());
  $template_collection = $actionPlugin->getTemplateCollection();

  // Get the email templates so we can modify them
  $templates = $template_collection->getTemplates();

  /** @var \Drupal\courier\Entity\Email $mail_template */
  $mail_template = $templates[0];
  $mail_template->setSubject('Test');
  $mail_template->setBody('Greetings, [identity:label]');
  $mail_template->save();

  // Save the mail template collection for this event.
  $context = $entityManager->getStorage('courier_context')
    ->load('rng_registration_' . $node->getEntityTypeId());
  if (!$context) {
    throw new \Exception(sprintf('No context available for %s', $node->getEntityTypeId()));
  }
  $template_collection->setContext($context);
  $template_collection->setOwner($node);
  $template_collection->save();

  // Set the action to send mail on new registrations.
  $action = RuleComponent::create([])
    ->setPluginId($actionPlugin->getPluginId())
    ->setConfiguration($actionPlugin->getConfiguration())
    ->setType('action');
  $rule = Rule::create([
    'event' => array('entity' => $node),
    'trigger_id' => 'entity:registration:new',
  ]);
  // Make rule active
  $rule->setIsActive(TRUE)
    ->save();
  // Save the action
  $action->setRule($rule)->save();
}

andreasradloff avatar Jul 12 '16 08:07 andreasradloff

@dpi do you think the code above helps in achieving the default message for new events feature?

kclarkson avatar Sep 21 '16 15:09 kclarkson

The above is a good stopgap measure, however I would make use of the default rules system introduced by #51

UI has to be built, the eventmeta class would need to be retrofitted to be aware of these defaults messages. Some other things I'm sure, but I'm not in the correct headspace.

At this point I dont have time scheduled for RNG for the near future.

dpi avatar Sep 22 '16 05:09 dpi

I think I'd prefer seeing an event subscriber and injecting the services needed, but this is a good pointer on what services are needed. Thxs for your work @andreasradloff

heddn avatar Jul 03 '17 21:07 heddn