-
Notifications
You must be signed in to change notification settings - Fork 7
/
message_ui.module
384 lines (333 loc) · 11.7 KB
/
message_ui.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
<?php
/**
* @file
* Contains Drupal\message_ui\message_ui.module.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\message\Entity\Message;
use Drupal\message_ui\Plugin\QueueWorker\MessageArgumentsWorker;
use Drupal\Core\Url;
/**
* Grant permission for the operation upon message.
*/
const MESSAGE_UI_ALLOW = TRUE;
/**
* Deny permission for the operation upon message.
*/
const MESSAGE_UI_DENY = TRUE;
/**
* Implements hook_entity_insert().
*/
function message_ui_entity_insert(EntityInterface $entity) {
if ($entity->getEntityTypeId() != 'field_config') {
return;
}
if ($entity->get('entity_type') != 'message') {
return;
}
// A new field was attached to the message template. We will make sure it will
// appear in message form.
\Drupal::service('message_ui.field_display_manager')->SetFieldsDisplay($entity->get('bundle'));
}
/**
* Implements hook_entity_base_field_info_alter().
*
* Extend the message entity type's field by providing display handlers.
*/
function message_ui_entity_base_field_info_alter(&$fields, EntityTypeInterface $entity_type) {
// Alter the uid and created field to include display settings.
if ($entity_type->id() != 'message') {
return;
}
if (!empty($fields['uid'])) {
/* @var Drupal\Core\Field\BaseFieldDefinition $fields['uid'] */
$fields['uid']->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'author',
'weight' => 0,
])
->setDisplayOptions('form', [
'type' => 'entity_reference_autocomplete',
'weight' => 5,
'#group' => 'advanced',
'settings' => [
'match_operator' => 'CONTAINS',
'size' => '60',
'placeholder' => '',
],
])
->setDisplayConfigurable('form', TRUE)
->setRequired(TRUE);
}
if (!empty($fields['created'])) {
$fields['created']->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'timestamp',
'weight' => 0,
])
->setDisplayOptions('form', [
'type' => 'datetime_timestamp',
'weight' => 10,
])
->setDisplayConfigurable('form', TRUE);
}
}
/**
* Implements hook_entity_type_alter().
*
* Extend the message entity type by providing form handlers.
*/
function message_ui_entity_type_alter(array &$entity_types) {
if (!isset($entity_types['message'])) {
return;
}
/* @var $message_config \Drupal\Core\Config\Entity\ConfigEntityType */
$entity_types['message']
->setAccessClass('Drupal\message_ui\MessageAccessControlHandler')
->setHandlerClass('view_builder', 'Drupal\message\MessageViewBuilder')
->setFormClass('default', 'Drupal\message_ui\Form\MessageForm')
->setFormClass('add', 'Drupal\message_ui\Form\MessageForm')
->setFormClass('edit', 'Drupal\message_ui\Form\MessageForm')
->setFormClass('delete', 'Drupal\message_ui\Form\MessageDeleteForm')
->setLinkTemplate('canonical', '/message/{message}')
->setLinkTemplate('edit-form', '/message/{message}/edit')
->setLinkTemplate('delete-form', '/message/{message}/delete');
}
/**
* Implements hook_theme().
*/
function message_ui_theme() {
return [
'message_add_list' => [
'variables' => ['content' => NULL],
],
];
}
/**
* Prepares variables for list of available message templates.
*
* Default template: message-add-list.html.twig.
*
* @param array $variables
* An associative array containing:
* - content: An array of message templates.
*
* @see message_add_page()
*/
function template_preprocess_message_add_list(array &$variables) {
$variables['templates'] = [];
if (!empty($variables['content'])) {
foreach ($variables['content'] as $template) {
$variables['templates'][$template->id()] = [
'template' => $template->id(),
'add_link' => \Drupal::l($template->label(), new Url('message_ui.add', ['message_template' => $template->id()])),
'description' => [
'#markup' => $template->getDescription(),
],
];
}
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function message_ui_form_message_system_settings_alter(&$form, FormStateInterface $form_state) {
$form['update_tokens'] = [
'#type' => 'fieldset',
'#itle' => t('Token update settings'),
];
$message_ui_settings = \Drupal::config('message_ui.settings');
$form['update_tokens']['update_tokens_update_tokens'] = [
'#type' => 'checkbox',
'#title' => t('Update messages arguments'),
'#description' => t('When editing a message template, the user can add or delete arguments. When this is checked, you can choose how to update to messages arguments.'),
'#default_value' => $message_ui_settings->get('update_tokens.update_tokens'),
];
$form['update_tokens']['update_tokens_how_to_act'] = [
'#type' => 'select',
'#title' => t('Choose how to act'),
'#default_value' => $message_ui_settings->get('update_tokens.how_to_act'),
'#options' => [
'update_when_removed' => t('Update messages when tokens are removed'),
'update_when_added' => t('Update messages when tokens are added'),
],
'#states' => [
'visible' => [
':input[name="update_tokens_update_tokens"]' => ['checked' => TRUE],
],
],
];
$form['update_tokens']['update_tokens_how_to_update'] = [
'#type' => 'select',
'#title' => t('Choose how to update the messages'),
'#default_value' => $message_ui_settings->get('update_tokens.how_to_update'),
'#options' => [
'update_with_batch' => t('Update messages with batch API'),
'update_with_item' => t('Update messages with queue item'),
],
'#states' => [
'visible' => [
':input[name="update_tokens_update_tokens"]' => ['checked' => TRUE],
],
],
];
$form['update_tokens']['update_tokens_number_items'] = [
'#type' => 'textfield',
'#size' => '10',
'#title' => t('Items to process each time.'),
'#description' => t('Choose how much items to process each iteration.'),
'#default_value' => $message_ui_settings->get('update_tokens.number_items'),
'#states' => [
'visible' => [
':input[name="update_tokens_update_tokens"]' => ['checked' => TRUE],
],
],
];
$form['message_ui_show_preview'] = [
'#type' => 'checkbox',
'#title' => t('Show/hide preview'),
'#default_value' => $message_ui_settings->get('show_preview'),
'#description' => t('Show/hide the text of the message when editing an instance of the message.'),
];
$form['#submit'][] = 'message_ui_form_message_system_settings_submit';
}
/**
* Submit handler for message_user_admin_settings.
*
* @param array $form
* FAPI element.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state object.
*/
function message_ui_form_message_system_settings_submit(array $form, FormStateInterface $form_state) {
$form_values = $form_state->getValues();
$config = \Drupal::configFactory()->getEditable('message_ui.settings');
$config
->set('update_tokens.update_tokens', $form_values['update_tokens_update_tokens'])
->set('update_tokens.how_to_act', $form_values['update_tokens_how_to_act'])
->set('update_tokens.how_to_update', $form_values['update_tokens_how_to_update'])
->set('update_tokens.number_items', $form_values['update_tokens_number_items'])
->set('show_preview', $form_values['message_ui_show_preview'])
->save();
}
/**
* Implements hook_entity_update().
*
* Submit handler for updating the arguments number.
*
* When a message template is been edited, there could be a change in the
* arguments of the message - added or removed.
* If this has been defined, we need to update the arguments of the other
* messages. This will be achieved by in two steps:
* 1. Load an instance of the message from the same template
* 2. Count the number of the arguments and if there is a difference between the
* number of the arguments from the old message to the current one - create
* a batch or a queue and update the messages.
*/
function message_ui_entity_update(EntityInterface $entity) {
$type = $entity->getEntityType()->id();
if ($type != 'message_template') {
return FALSE;
}
$query = \Drupal::entityQuery('message');
$result = $query
->condition('template', $entity->getTemplate())
->range(0, 1)
->sort('mid', 'DESC')
->execute();
// There is no messages of this template.
if (empty($result)) {
return FALSE;
}
$keys = array_keys($result);
$message = Message::load(reset($keys));
$new_arguments = MessageArgumentsWorker::getArguments($entity->getTemplate());
$old_arguments_number = count($message->getArguments());
$new_arguments_number = count($new_arguments);
$message_ui_settings_config = \Drupal::config('message_ui.settings');
$how_to_act = $message_ui_settings_config->get('update_tokens.how_to_act');
$update['when_added'] = $old_arguments_number < $new_arguments_number && $how_to_act == 'update_when_added';
$update['when_removed'] = $old_arguments_number > $new_arguments_number && $how_to_act == 'update_when_removed';
if (!($update['when_added'] || $update['when_removed'])) {
return FALSE;
}
$item_to_process = $message_ui_settings_config->get('update_tokens.number_items');
$how_to_update = $message_ui_settings_config->get('update_tokens.how_to_update');
if ($how_to_update == 'update_with_batch') {
// Get all the messages.
$query = \Drupal::entityQuery('message');
$result = $query
->condition('template', $entity->getTemplate())
->sort('mid', 'DESC')
->execute();
$chunks = array_chunk(array_keys($result), $item_to_process);
// @todo : Correct location for operations callback?
$operations = [];
foreach ($chunks as $chunk) {
$operations[] = [
'\Drupal\message_ui\Plugin\QueueWorker\MessageArgumentsWorker::argumentsUpdate',
[$chunk, $new_arguments],
];
}
// @todo : Correct location for finished callback?
// Set the batch.
$batch = [
'operations' => $operations,
'finished' => '\Drupal\message_ui\Plugin\QueueWorker\MessageArgumentsWorker::messageArgumentsUpdate',
'title' => t('Updating the messages arguments.'),
'init_message' => t('Start process messages.'),
'progress_message' => t('Processed @current out of @total.'),
'error_message' => t('Example Batch has encountered an error.'),
];
batch_set($batch);
batch_process('admin/structure/messages');
}
elseif ($how_to_update == 'update_with_item') {
// Define the queue item data.
$data = [
'template' => $entity->getTemplate(),
'last_mid' => 0,
'new_arguments' => $new_arguments,
'item_to_process' => $item_to_process,
];
// Set the queue worker.
$queue = \Drupal::queue('message_ui_arguments');
return $queue->createItem($data);
}
return NULL;
}
/**
* Implements hook_entity_load().
*/
function message_ui_entity_load(array &$entities, $entity_type_id) {
static $saved;
if (!empty($saved)) {
return;
}
if ($entity_type_id != 'view') {
return;
}
foreach ($entities as &$entity) {
if ($entity->id() != 'message') {
continue;
}
$display = $entity->get('display');
if (!empty($display['default']['display_options']['fields']['message_ui_contextual_links'])) {
$saved = TRUE;
continue;
}
$display['default']['display_options']['fields']['message_ui_contextual_links'] = [
'id' => 'message_ui_contextual_links',
'table' => 'message',
'field' => 'message_ui_contextual_links',
'entity_type' => 'message',
'label' => 'Operations',
];
$entity->set('display', $display);
// Mark the entity as saved.
$saved = TRUE;
$entity->save();
}
}