Skip to content
This repository has been archived by the owner on Feb 23, 2020. It is now read-only.

#166 add event type message template #198

Open
wants to merge 7 commits into
base: 8.x-1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions config/schema/rng.data_types.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,19 @@ rng_event_type_people_type:
entity_form_mode:
type: string
label: 'Form display mode used when the entity is created inline.'

rng_event_type_default_message:
type: mapping
mapping:
trigger:
type: string
label: 'Trigger'
status:
type: boolean
label: 'Whether this entity bundle can be created inline.'
subject:
type: string
label: 'Subject of the message.'
body:
type: string
label: 'Message body.'
5 changes: 5 additions & 0 deletions config/schema/rng.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ rng.event_type.*.*:
label: 'Types of people types allowed to be associated with this event type.'
sequence:
type: rng_event_type_people_type
default_messages:
type: sequence
label: 'Default messages for this event type.'
sequence:
type: rng_event_type_default_message

condition.plugin.rng_user_role:
type: condition.plugin.user_role
Expand Down
8 changes: 7 additions & 1 deletion rng.links.action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ rng.local_actions:
class: \Drupal\Core\Menu\LocalActionDefault
deriver: \Drupal\rng\Plugin\Derivative\LocalActions

rng.default_messages.add_form:
route_name: entity.event_type.default_messages.add
title: 'Add message'
appears_on:
- entity.event_type.default_messages

rng.registration_type.add:
route_name: entity.registration_type.add
title: 'Add registration type'
Expand All @@ -18,4 +24,4 @@ rng.event_type.add:
route_name: entity.event_type.add
title: 'Add event type'
appears_on:
- rng.event_type.overview
- rng.event_type.overview
7 changes: 6 additions & 1 deletion rng.links.task.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ entity.event_type.access_defaults:
base_route: entity.event_type.edit_form
title: 'Access Defaults'

entity.event_type.default_messages:
route_name: entity.event_type.default_messages
base_route: entity.event_type.edit_form
title: 'Default messages'

# Registration type
entity.registration_type.edit_form:
title: 'Edit'
Expand Down Expand Up @@ -91,4 +96,4 @@ entity.rng_rule_component.edit:
rng.config.settings:
route_name: rng.config.settings
title: 'Settings'
base_route: rng.config.settings
base_route: rng.config.settings
7 changes: 7 additions & 0 deletions rng.module
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ function rng_entity_operation(EntityInterface $entity) {
]),
'weight' => 20,
);
$operations['default_messages'] = array(
'title' => t('Default messages'),
'url' => Url::fromRoute('entity.event_type.default_messages', [
'event_type' => $entity->id(),
]),
'weight' => 25,
);
}

return $operations;
Expand Down
20 changes: 20 additions & 0 deletions rng.routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,26 @@ entity.event_type.access_defaults:
requirements:
_permission: 'administer event types'

entity.event_type.default_messages:
path: '/admin/structure/rng/event_types/manage/{event_type}/messages'
defaults:
_entity_form: 'event_type.event_default_messages'
_title: 'Event type Default messages'
requirements:
_permission: 'administer event types'

entity.event_type.default_messages.add:
path: '/admin/structure/rng/event_types/manage/{event_type}/messages/add'
defaults:
_form: '\Drupal\rng\Form\EventTypeDefaultMessagesAddForm'
_title: 'Add message'
options:
parameters:
event_type:
type: entity:event_type
requirements:
_permission: 'administer event types'

entity.event_type.field_mapping:
path: '/admin/structure/rng/event_types/manage/{event_type}/field_mapping'
defaults:
Expand Down
23 changes: 23 additions & 0 deletions src/Entity/EventType.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
* "edit" = "Drupal\rng\Form\EventTypeForm",
* "delete" = "Drupal\rng\Form\EventTypeDeleteForm",
* "event_access_defaults" = "Drupal\rng\Form\EventTypeAccessDefaultsForm",
* "event_default_messages" = "Drupal\rng\Form\EventTypeDefaultMessagesListForm",
* "field_mapping" = "Drupal\rng\Form\EventTypeFieldMappingForm",
* }
* },
Expand Down Expand Up @@ -106,6 +107,13 @@ class EventType extends ConfigEntityBase implements EventTypeInterface {
*/
protected $people_types = [];

/**
* Default messages configured for this event type.
*
* @var array
*/
protected $default_messages = [];

/**
* Fields to add to event bundles.
*
Expand Down Expand Up @@ -189,6 +197,21 @@ function getDefaultRegistrantType() {
return $this->default_registrant;
}

/**
* {@inheritdoc}
*/
function getDefaultMessages() {
return $this->default_messages;
}

/**
* {@inheritdoc}
*/
function setDefaultMessages($messages) {
$this->default_messages = $messages;
return $this;
}

/**
* {@inheritdoc}
*/
Expand Down
82 changes: 81 additions & 1 deletion src/EventMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
use Drupal\Core\Entity\EntityInterface;
use Drupal\rng\Entity\Rule;
use Drupal\rng\Entity\RuleComponent;
use Drupal\courier\Entity\TemplateCollection;
use Drupal\courier\Service\CourierManagerInterface;
use Drupal\Core\Action\ActionManager;

/**
* Meta event wrapper for RNG.
Expand Down Expand Up @@ -65,6 +68,20 @@ class EventMeta implements EventMetaInterface {
*/
protected $eventManager;

/**
* The courier manager.
*
* @var \Drupal\courier\Service\CourierManagerInterface
*/
protected $courier_manager;

/**
* The action manager.
*
* @var \Drupal\Core\Action\ActionManager
*/
protected $action_manager;

/**
* Constructs a new EventMeta object.
*
Expand All @@ -80,16 +97,22 @@ class EventMeta implements EventMetaInterface {
* The RNG configuration service.
* @param \Drupal\rng\EventManagerInterface $event_manager
* The RNG event manager.
* @param \Drupal\courier\Service\CourierManagerInterface $courier_manager
* The courier manager.
* @param \Drupal\Core\Action\ActionManager $action_manager
* The action manager.
* @param \Drupal\Core\Entity\EntityInterface $entity
* The event entity.
*/
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) {
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) {
$this->entityManager = $entity_manager;
$this->configFactory = $config_factory;
$this->selectionPluginManager = $selection_plugin_manager;
$this->identityChannelManager = $identity_channel_manager;
$this->rngConfiguration = $rng_configuration;
$this->eventManager = $event_manager;
$this->courier_manager = $courier_manager;
$this->action_manager = $action_manager;
$this->entity = $entity;
}

Expand All @@ -104,6 +127,8 @@ public static function createInstance(ContainerInterface $container, EntityInter
$container->get('plugin.manager.identity_channel'),
$container->get('rng.configuration'),
$container->get('rng.event_manager'),
$container->get('courier.manager'),
$container->get('plugin.manager.action'),
$entity
);
}
Expand Down Expand Up @@ -608,4 +633,59 @@ protected function entityTypeHasBundles($entity_type_id) {
return ($entity_type->getBundleEntityType() !== NULL);
}

/**
* {@inheritdoc}
*/
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();
}
}
}
}

}
5 changes: 5 additions & 0 deletions src/EventMetaInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -366,4 +366,9 @@ public function identitiesCanRegister($entity_type, array $entity_ids);
*/
function addDefaultAccess();

/**
* Create messages for Event from Default messages for this Event Type.
*/
function createDefaultEventMessages();

}
15 changes: 15 additions & 0 deletions src/EventTypeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,21 @@ function setAllowCustomRules($allow);
*/
function getDefaultRegistrantType();

/**
* Default messages configured for this event type.
*
* @return array
*/
function getDefaultMessages();

/**
* Set default messages for this event type.
*
* @param array $messages
* Default messages array.
*/
function setDefaultMessages($messages);

/**
* Whether a identity type can be created.
*
Expand Down
Loading