dpi / rng

RNG is a Drupal module enabling people to register for events.
https://www.drupal.org/project/rng
GNU General Public License v2.0
15 stars 23 forks source link

Add ability to create default messages for new events. #81

Open dpi opened 8 years ago

dpi commented 8 years ago

From #79 / LP

andreasradloff commented 8 years ago

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();
}
kclarkson commented 7 years ago

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

dpi commented 7 years ago

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.

heddn commented 7 years ago

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