CONTENTS OF THIS FILE
- Introduction
- Usage
- Examples
INTRODUCTION
Message recipient module allows you to collect recipients for Messages.
USAGE
- To manage recipients' collectors for a message template, you should first enable message_recipient_ui module.
- After creating a message template, you could add multiple recipients' collectors to the template using the UI.
- The module comes with a service that allows you to collect recipients. See examples section.
Examples
Example #1: On entity insert, collect users having a specific role:
- Having a message template that has a 'Users by roles' recipient collector configured:
/**
* Implements hook_entity_insert().
*/
function YOUR_MODULE_entity_insert(EntityInterface $entity): void {
$message = Message::create(['template' => 'example_create_entity', 'uid' => $entity->get('uid')]);
$message->save();
// Collect recipients.
/** @var \Drupal\message_recipient\MessageRecipientsCollector $message_recipients_collector */
$message_recipients_collector = \Drupal::service('message_recipient.recipients_collector');
$recipients = $message_recipients_collector->getRecipients($message);
}
Example #2: On entity update, collect an entity owner:
- Enable message_recipient_entity sub-module.
- Having a message template that has an 'Entity owner' recipient collector configured:
/**
* Implements hook_entity_update().
*/
function YOUR_MODULE_entity_update(EntityInterface $entity): void {
$message = Message::create(['template' => 'example_update_entity', 'uid' => $entity->get('uid')]);
$message->set('message_associated_entity', [
'target_type' => $entity->getEntityTypeId(),
'target_id' => $entity->id(),
]);
$message->save();
// Collect recipients.
/** @var \Drupal\message_recipient\MessageRecipientsCollector $message_recipients_collector */
$message_recipients_collector = \Drupal::service('message_recipient.recipients_collector');
$recipients = $message_recipients_collector->getRecipients($message);
}