-
Notifications
You must be signed in to change notification settings - Fork 17
/
message_notify.module
66 lines (57 loc) · 2.01 KB
/
message_notify.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?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();
}
}