This repository has been archived by the owner on Jan 9, 2024. It is now read-only.
forked from Gizra/message
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.module
233 lines (198 loc) · 7.03 KB
/
message.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php
/**
* @file
* API functions to manipulate messages.
*/
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Render\Element;
use Drupal\message\Entity\Message;
use Drupal\message\Entity\MessageTemplate;
use Drupal\message\MessagePurgeInterface;
/**
* Implements hook_entity_delete().
*
* Handles messages deletion when referenced entities are being deleted.
*/
function message_entity_delete(EntityInterface $entity) {
$type = $entity->getEntityType()->id();
if ($type == 'message') {
// Why not message?
return;
}
$types = \Drupal::config('message.settings')->get('delete_on_entity_delete');
if (!$types || !in_array($type, $types)) {
return;
}
// List of messages to delete.
$deletable_mids = [];
// List of messages that might be deleted;
// Messages with references to fields with multiple cardinality will be stored
// in $check_mids in order to check if the entity being deleted is the last
// one referenced by a given field.
// Keyed by message ID, pointing to array of the relevant field names.
$check_mids = [];
/** @var \Drupal\Core\Field\FieldStorageDefinitionInterface[] $fields */
$fields = \Drupal::entityTypeManager()->getStorage('field_storage_config')->loadMultiple();
// Search for fields in which messages referenced the deleted entity.
foreach ($fields as $field) {
if ($field->getTargetEntityTypeId() != 'message') {
// This field isn't used in any message.
continue;
}
// Only delete messages due to referenced entity or referenced taxonomy term
// deletion.
if ($field->getType() != 'entity_reference') {
continue;
}
// Only delete references if the referenced entity is the correct type.
if ($field->getSetting('target_type') != $entity->getEntityTypeId()) {
continue;
}
$query = \Drupal::entityQuery('message');
$results = $query
->condition($field->getName(), $entity->id())
->execute();
if (empty($results)) {
continue;
}
if ($field->getCardinality() == 1) {
$deletable_mids = array_merge($deletable_mids, $results);
}
else {
foreach ($results as $id) {
$check_mids[$id][] = $field->getName();
}
}
}
$deletable_mids = array_values($deletable_mids);
// Check messages with multiple cardinality references; Only delete such
// messages if the entity being deleted is the last one referenced by the
// message.
if ($check_mids) {
$queue_set = [];
$count = 0;
$num_mids = count($check_mids);
foreach ($check_mids as $id => $field_names) {
// If it already qualified for deletion based on one field, there's no
// need to check multi-valued fields.
if (!in_array($id, $deletable_mids)) {
$queue_set[$id] = $field_names;
}
if ($queue_set && ($count % MessagePurgeInterface::MESSAGE_DELETE_SIZE == 0 || $count == $num_mids)) {
\Drupal::queue('message_check_delete')->createItem($queue_set);
$queue_set = [];
}
$count++;
}
}
if ($deletable_mids) {
$num_items = ceil(count($deletable_mids) / MessagePurgeInterface::MESSAGE_DELETE_SIZE);
for ($i = 0; $i < $num_items; $i++) {
$queue_set = array_slice($deletable_mids, $i * MessagePurgeInterface::MESSAGE_DELETE_SIZE, MessagePurgeInterface::MESSAGE_DELETE_SIZE);
\Drupal::queue('message_delete')->createItem($queue_set);
}
}
}
/**
* Implements hook_cron().
*
* Fetch all message templates and purge old messages.
*/
function message_cron() {
/** @var \Drupal\message\MessagePurgeOrchestrator $purge_orchestrator */
$purge_orchestrator = \Drupal::service('message.purge_orchestrator');
$purge_orchestrator->purgeAllTemplateMessages();
}
/**
* Usort callback; Order the form elements by their weight.
*/
function message_order_text_weight($a, $b) {
if ($a['_weight'] == $b['_weight']) {
return 0;
}
return ($a['_weight'] < $b['_weight']) ? -1 : 1;
}
/**
* Implements hook_entity_extra_field_info().
*/
function message_entity_extra_field_info() {
$extra = [];
/** @var \Drupal\message\Entity\MessageTemplate[] $bundles */
$bundles = MessageTemplate::loadMultiple();
foreach ($bundles as $bundle) {
foreach (array_keys($bundle->getText()) as $delta) {
if (!is_int($delta)) {
// The get text holds also the translated text. Since we hold only need
// the number of partials we don't need to include delta of texts.
continue;
}
$params = ['%number' => $delta];
$extra['message'][$bundle->id()]['display']['partial_' . $delta] = [
'label' => t('Partial %number', $params),
'description' => t('Holds the partial text at position %number', $params),
'weight' => 0,
];
}
}
return $extra;
}
/**
* Implements hook_theme().
*/
function message_theme() {
return [
'message' => [
'render element' => 'elements',
],
];
}
/**
* Prepares variables for message templates.
*
* Default template: message.html.twig.
*
* @param array $variables
* An associative array containing:
* - elements: An array of elements to display in view mode.
* - message: The message object.
* - view_mode: View mode; e.g., 'full', 'teaser', etc.
*/
function template_preprocess_message(array &$variables) {
$variables['view_mode'] = $variables['elements']['#view_mode'];
// Provide a distinct $teaser boolean.
$variables['teaser'] = $variables['view_mode'] == 'teaser';
$variables['message'] = $variables['elements']['#message'];
/** @var \Drupal\message\MessageInterface $message */
$message = $variables['message'];
$variables['date'] = drupal_render($variables['elements']['created']);
unset($variables['elements']['created']);
$variables['author_name'] = drupal_render($variables['elements']['uid']);
unset($variables['elements']['uid']);
$variables['url'] = $message->url('canonical', [
'language' => $message->language(),
]);
// The 'page' variable is set to TRUE in two occasions:
// - The view mode is 'full' and we are on the 'message.view' route.
// - The message is in preview and view mode is either 'full' or 'default'.
$variables['page'] = ($variables['view_mode'] == 'full' || $variables['view_mode'] == 'default');
// Helpful $content variable for templates.
$variables += ['content' => []];
foreach (Element::children($variables['elements']) as $key) {
$variables['content'][$key] = $variables['elements'][$key];
}
}
/**
* Implements hook_theme_suggestions_HOOK().
*/
function message_theme_suggestions_message(array $variables) {
$suggestions = [];
/** @var \Drupal\message\MessageInterface $message */
$message = $variables['elements']['#message'];
$sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');
$suggestions[] = 'message__' . $sanitized_view_mode;
$suggestions[] = 'message__' . $message->bundle();
$suggestions[] = 'message__' . $message->bundle() . '__' . $sanitized_view_mode;
$suggestions[] = 'message__' . $message->id();
$suggestions[] = 'message__' . $message->id() . '__' . $sanitized_view_mode;
return $suggestions;
}