View source
<?php
namespace Drupal\rng\Plugin\Action;
use Drupal\Core\Action\ConfigurableActionBase;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\rng\EventManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\courier\Entity\TemplateCollection;
use Drupal\courier\Service\CourierManagerInterface;
class CourierTemplateCollection extends ConfigurableActionBase implements ContainerFactoryPluginInterface {
protected $entityManager;
protected $eventManager;
protected $courierManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityManagerInterface $entity_manager, EventManagerInterface $event_manager, CourierManagerInterface $courier_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityManager = $entity_manager;
$this->eventManager = $event_manager;
$this->courierManager = $courier_manager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity.manager'), $container
->get('rng.event_manager'), $container
->get('courier.manager'));
}
public function defaultConfiguration() {
return [
'template_collection' => NULL,
];
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
if ($template_collection = $this
->getTemplateCollection()) {
$form['template_collection']['#markup'] = $this
->t('Template collection #@id', [
'@id' => $template_collection
->id(),
]);
}
else {
drupal_set_message('No template collection entity found.', 'warning');
}
return $form;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$configuration = $this
->getConfiguration();
if (!isset($configuration['template_collection'])) {
$template_collection = TemplateCollection::create();
if ($template_collection
->save()) {
$this->courierManager
->addTemplates($template_collection);
$template_collection
->save();
}
$this->configuration['template_collection'] = $template_collection
->id();
}
}
public function execute($context = NULL) {
if (!isset($context['registrations'])) {
return;
}
if (!is_array($context['registrations'])) {
return;
}
if ($collection_original = $this
->getTemplateCollection()) {
foreach ($context['registrations'] as $registration) {
$options = [];
if (($event = $registration
->getEvent()) instanceof EntityInterface) {
$event_meta = $this->eventManager
->getMeta($event);
$options['channels']['courier_email']['reply_to'] = $event_meta
->getReplyTo();
$collection_original
->setTokenValue($event
->getEntityTypeId(), $event);
}
$collection = clone $collection_original;
$collection
->setTokenValue('registration', $registration);
foreach ($registration
->getRegistrants() as $registrant) {
$identity = $registrant
->getIdentity();
$this->courierManager
->sendMessage($collection, $identity, $options);
}
}
}
}
public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
return FALSE;
}
public function getTemplateCollection() {
if (isset($this->configuration['template_collection'])) {
return $this->entityManager
->getStorage('courier_template_collection')
->load($this->configuration['template_collection']);
}
return NULL;
}
}