message_notify.module in Message Notify 8
Same filename and directory in other branches
Message notify module.
File
message_notify.moduleView source
<?php
/**
* @file
* Message notify module.
*/
use Drupal\Core\Entity\Entity\EntityViewMode;
/**
* Implements hook_mail().
*
* Set's the message subject and body as configured.
*/
function message_notify_mail($key, &$message, $params) {
$message['subject'] = $params['mail_subject'];
$message['body'][] = $params['mail_body'];
}
/**
* Implements hook_entity_bundle_create().
*
* We cannot easily set the the visibilty of extra fields, so we set the
* bundle settings upon creation of new message bundle.
*/
function message_notify_entity_bundle_create($entity_type_id, $bundle) {
if ($entity_type_id != 'message') {
return;
}
// Do nothing if these view modes are not yet available.
if (!EntityViewMode::load('message.mail_body') || !EntityViewMode::load('message.mail_subject')) {
return;
}
// If this bundle is being created from a .yml file, ignore.
if (\Drupal::isConfigSyncing()) {
return;
}
$storage = \Drupal::entityTypeManager()
->getStorage('entity_view_display');
/** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $default */
if (method_exists(\Drupal::service('entity_display.repository'), 'getViewDisplay')) {
$default = \Drupal::service('entity_display.repository')
->getViewDisplay($entity_type_id, $bundle);
}
else {
// Pre-Drupal 8.8.
$default = entity_get_display($entity_type_id, $bundle, 'default');
}
// Setup the subject/title display mode if it doesn't exist.
if (!$storage
->load($entity_type_id . '.' . $bundle . '.mail_subject')) {
$display = $default
->createCopy('mail_subject');
$display
->set('content', [
'partial_0' => [
'weight' => 0,
],
]);
$display
->set('hidden', [
'partial_1' => TRUE,
]);
$display
->save();
}
// Setup the body display if it doesn't exist.
if (!$storage
->load($entity_type_id . '.' . $bundle . '.mail_body')) {
$display = $default
->createCopy('mail_body');
$display
->set('content', [
'partial_1' => [
'weight' => 0,
],
]);
$display
->set('hidden', [
'partial_0' => TRUE,
]);
$display
->save();
}
}
Functions
Name | Description |
---|---|
message_notify_entity_bundle_create | Implements hook_entity_bundle_create(). |
message_notify_mail | Implements hook_mail(). |