-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathquant.module
487 lines (409 loc) · 14 KB
/
quant.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
<?php
/**
* @file
* Hook implementations for Quant.
*/
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Site\Settings;
use Drupal\Core\Url;
use Drupal\node\NodeInterface;
use Drupal\quant\Exception\TokenValidationDisabledException;
use Drupal\quant\Plugin\QueueItem\RouteItem;
use Drupal\quant\QuantQueueFactory;
use Drupal\quant\Seed;
use Drupal\quant\Utility;
/**
* Implements hook_menu_local_tasks_alter().
*/
function quant_menu_local_tasks_alter(&$data, $route_name) {
if (!\Drupal::requestStack()->getCurrentRequest()->headers->get('quant-token')) {
return;
}
if (\Drupal::currentUser()->isAnonymous()) {
unset($data['tabs']);
}
}
/**
* Implements hook_node_insert().
*/
function quant_node_insert(EntityInterface $entity) {
$quant_enabled = \Drupal::config('quant.settings')->get('quant_enabled');
$quant_node_enabled = \Drupal::config('quant.settings')->get('quant_enabled_nodes');
if (!$quant_enabled || !$quant_node_enabled) {
return;
}
// Exclude draft revisions from Quant if enabled.
$disable_drafts = \Drupal::config('quant.settings')->get('disable_content_drafts');
if ($disable_drafts && !$entity->isPublished()) {
return;
}
$context = [
'callback' => '_quant_entity_update_op',
'args' => $entity,
];
drupal_register_shutdown_function('quant_shutdown', $context);
}
/**
* Implements hook_node_update().
*/
function quant_node_update(EntityInterface $entity) {
$quant_enabled = \Drupal::config('quant.settings')->get('quant_enabled');
$quant_node_enabled = \Drupal::config('quant.settings')->get('quant_enabled_nodes');
if (!$quant_enabled || !$quant_node_enabled) {
return;
}
// If entity default revision is unpublished then unpublish in Quant.
if (!$entity->isPublished() && $entity->isDefaultRevision()) {
// Trigger an unpublish event.
Seed::unpublishNode($entity);
}
// Exclude draft revisions from Quant if enabled.
$disable_drafts = \Drupal::config('quant.settings')->get('disable_content_drafts');
if ($disable_drafts && !$entity->isPublished()) {
return;
}
$context = [
'callback' => '_quant_entity_update_op',
'args' => $entity,
];
drupal_register_shutdown_function('quant_shutdown', $context);
}
/**
* Implements hook_taxonomy_term_insert().
*/
function quant_taxonomy_term_insert(EntityInterface $entity) {
$quant_enabled = \Drupal::config('quant.settings')->get('quant_enabled');
$quant_taxonomy_enabled = \Drupal::config('quant.settings')->get('quant_enabled_taxonomy');
if (!$quant_enabled || !$quant_taxonomy_enabled) {
return;
}
// Exclude draft revisions from Quant if enabled.
$disable_drafts = \Drupal::config('quant.settings')->get('disable_content_drafts');
if ($disable_drafts && !$entity->isPublished()) {
return;
}
$context = [
'callback' => '_quant_entity_update_op',
'args' => $entity,
];
drupal_register_shutdown_function('quant_shutdown', $context);
}
/**
* Implements hook_taxonomy_term_update().
*/
function quant_taxonomy_term_update(EntityInterface $entity) {
$quant_enabled = \Drupal::config('quant.settings')->get('quant_enabled');
$quant_taxonomy_enabled = \Drupal::config('quant.settings')->get('quant_enabled_taxonomy');
if (!$quant_enabled || !$quant_taxonomy_enabled) {
return;
}
// If entity default revision is unpublished then unpublish in Quant.
if (!$entity->isPublished() && $entity->isDefaultRevision()) {
// Trigger an unpublish event.
Seed::unpublishTaxonomyTerm($entity);
}
// Exclude draft revisions from Quant if enabled.
$disable_drafts = \Drupal::config('quant.settings')->get('disable_content_drafts');
if ($disable_drafts && !$entity->isPublished()) {
return;
}
$context = [
'callback' => '_quant_entity_update_op',
'args' => $entity,
];
drupal_register_shutdown_function('quant_shutdown', $context);
}
/**
* Implements hook_entity_predelete().
*/
function quant_entity_predelete(EntityInterface $entity) {
$quant_enabled = \Drupal::config('quant.settings')->get('quant_enabled');
if (!$quant_enabled) {
return;
}
// This needs to not be a shutdown hook and also needs to be on
// the predelete hook as the path alias is removed in the entity
// delete clean-up.
_quant_entity_delete_op($entity);
}
/**
* Intermediary shutdown function.
*
* This is used to register another shutdown function. Some modules perform
* actions during the shutdown to ensure that we have the correct hydration
* state we use this to register the seed after all other functions.
*/
function quant_shutdown(array $context = []) {
$quant_enabled = \Drupal::config('quant.settings')->get('quant_enabled');
if (!$quant_enabled) {
return;
}
if (is_callable($context['callback'])) {
drupal_register_shutdown_function($context['callback'], $context['args']);
}
}
/**
* Implements hook_node_access().
*/
function quant_node_access(NodeInterface $node, $op, AccountInterface $account) {
$request = \Drupal::request();
// Both revision & token headers need to be present to alter access.
if (!$request->headers->has('quant-revision') || !$request->headers->has('quant-token')) {
return AccessResult::neutral();
}
$options = ['absolute' => FALSE];
$url = Url::fromRoute('entity.node.canonical', ['node' => $node->id()], $options)->toString();
try {
// Token validation for node access cannot be strict as this will be called
// for embedded entities on a page the token process is bound to requests
// if the incoming request has a valid token we assume that all
// embedded entities are valid.
//
// @see Drupal\quant\EventSubscriber\TokenAccessSubscriber
\Drupal::service('quant.token_manager')->validate($url, FALSE);
}
catch (TokenValidationDisabledException $e) {
// Allow access when token validation is disabled.
}
catch (\Exception $e) {
return AccessResult::neutral();
}
// If the token validation didn't trigger an exception - then the
// token is valid and can be continued.
return AccessResult::allowed();
}
/**
* Implements hook_redirect_presave().
*/
function quant_redirect_presave($redirect) {
$quant_enabled = \Drupal::config('quant.settings')->get('quant_enabled');
$quant_redirect_enabled = \Drupal::config('quant.settings')->get('quant_enabled_redirects');
if (!$quant_enabled || !$quant_redirect_enabled) {
return;
}
Seed::seedRedirect($redirect);
}
/**
* Implements hook_redirect_delete().
*/
function quant_redirect_delete($redirect) {
$quant_enabled = \Drupal::config('quant.settings')->get('quant_enabled');
$quant_redirect_enabled = \Drupal::config('quant.settings')->get('quant_enabled_redirects');
if (!$quant_enabled || !$quant_redirect_enabled) {
return;
}
Seed::deleteRedirect($redirect);
}
/**
* Entity update operation hook.
*
* @param Drupal\Core\Entity\EntityInterface $entity
* The entity.
*
* This should be registered as a shutdown function so that it
* can operate after the db_transaction has finished.
*
* @todo Entity support.
*/
function _quant_entity_update_op(EntityInterface $entity) {
$langcode = $entity->language()->getId();
switch ($entity->getEntityTypeId()) {
case 'node':
Seed::seedNode($entity, $langcode);
break;
case 'taxonomy_term':
Seed::seedTaxonomyTerm($entity, $langcode);
break;
}
}
/**
* Entity delete operation hook.
*
* @param Drupal\Core\Entity\EntityInterface $entity
* The entity.
*
* Used to trigger an unpublish from the Quant API.
*
* @todo Entity support.
*/
function _quant_entity_delete_op(EntityInterface $entity) {
switch ($entity->getEntityTypeId()) {
case 'node':
Seed::unpublishNode($entity);
break;
case 'path_alias':
Seed::unpublishPathAlias($entity);
break;
case 'taxonomy_term':
Seed::unpublishTaxonomyTerm($entity);
break;
}
}
/**
* Implements hook_ENTITY_TYPE_translation_delete().
*/
function quant_node_translation_delete($entity) {
$quant_enabled = \Drupal::config('quant.settings')->get('quant_enabled');
$quant_node_enabled = \Drupal::config('quant.settings')->get('quant_enabled_nodes');
if (!$quant_enabled || !$quant_node_enabled) {
return;
}
Seed::unpublishNode($entity);
}
/**
* Process the queue.
*
* @param array $context
* The batch context.
*/
function quant_process_queue(array &$context) {
$factory = QuantQueueFactory::getInstance();
$manager = \Drupal::service('plugin.manager.queue_worker');
$queue = $factory->get('quant_seed_worker');
$worker = $manager->createInstance('quant_seed_worker');
$item = $queue->claimItem();
if (empty($context['sandbox']['progress'])) {
$context['sandbox']['progress'] = 0;
$context['sandbox']['total'] = $queue->numberOfItems();
}
if (!$item) {
$context['finished'] = 1;
return FALSE;
}
$worker->processItem($item->data);
$queue->deleteItem($item);
$context['sandbox']['progress']++;
$context['message'] = t('Processed @i of @t', [
'@i' => $context['sandbox']['progress'],
'@t' => $context['sandbox']['total'],
]);
$context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['total'] >= 1 ? 0.9 : $context['sandbox']['progress'] / $context['sandbox']['total'];
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function quant_form_system_site_information_settings_alter(&$form, FormStateInterface $form_state) {
$form['#submit'][] = 'quant_special_pages';
}
/**
* Update the special pages when the form is saved.
*/
function quant_special_pages() {
foreach (Utility::getSpecialPages() as $route) {
$item = new RouteItem(['route' => $route]);
$item->send();
\Drupal::logger('quant')->notice("Sending route: @route", ['@route' => $route]);
}
}
/**
* Implements hook_modules_installed().
*/
function quant_modules_installed($modules) {
if (in_array('workbench_moderation', $modules)) {
$config = \Drupal::configFactory()->getEditable('quant.settings');
$disabled = $config->get('disable_content_drafts');
// Only disable if not already disabled.
if (!$disabled) {
$config->set('disable_content_drafts', 1);
$config->save();
\Drupal::messenger()->addMessage(t('Quant draft content handling has been disabled because the Workbench Moderation module has been installed.'));
\Drupal::logger('quant')->notice('Quant draft content handling has been disabled because the Workbench Moderation module has been installed.');
}
}
}
/**
* Implements hook_modules_uninstalled().
*/
function quant_modules_uninstalled($modules) {
if (in_array('workbench_moderation', $modules)) {
$config = \Drupal::configFactory()->getEditable('quant.settings');
$disabled = $config->get('disable_content_drafts');
// Only show message if disabled.
if ($disabled) {
\Drupal::messenger()->addMessage(t('Quant draft content handling can be enabled now that the Workbench Moderation module has been uninstalled.'));
\Drupal::logger('quant')->notice('Quant draft content handling can be enabled now that the Workbench Moderation module has been uninstalled.');
}
}
}
/**
* Unpublish old alias if it changes.
*
* @param Drupal\Core\Entity\EntityInterface $pathAlias
* The path alias.
*/
function quant_path_alias_presave(EntityInterface $pathAlias) {
if (!$pathAlias || !$pathAlias->original) {
return;
}
// Original path alias.
$original_path = $pathAlias->original->get('path')->value;
$original_alias = $pathAlias->original->get('alias')->value;
$original_langcode = $pathAlias->original->get('langcode')->value;
// Current path alias.
$path = $pathAlias->get('path')->value;
$alias = $pathAlias->get('alias')->value;
$langcode = $pathAlias->get('langcode')->value;
// Unpublish original alias if it changed.
if ($original_path == $path && $langcode == $original_langcode && $original_alias != $alias) {
Seed::unpublishPathAlias($pathAlias->original);
}
}
/**
* Implements hook_preprocess_page().
*/
function quant_preprocess_page(&$variables) {
// Show the Quant page info block for non-admin pages if enabled.
$config = \Drupal::configFactory()->getEditable('quant.settings');
$enabled = $config->get('quant_show_page_info_block');
if ($enabled && !\Drupal::service('router.admin_context')->isAdminRoute()) {
$variables['page']['content']['#prefix'] = Utility::getPageInfo() . ($variables['page']['content']['#prefix'] ?? '');
}
// Add warning on Quant admin pages if queue factory is not set.
$current_path = \Drupal::requestStack()->getCurrentRequest()->getPathInfo();
if ((strpos($current_path, '/admin/config/quant') === 0) && Settings::get('queue_service_quant_seed_worker') != "quant.queue_factory") {
\Drupal::messenger()->addWarning(t('For the best performance, it is highly recommended you update <em>queue_service_quant_seed_worker</em> in your settings. See the <a href="https://docs.quantcdn.io/docs/integrations/drupal">Drupal docs</a> for setup information.'));
}
}
/**
* Implements hook_views_data_alter().
*/
function quant_views_data_alter(array &$data) {
$field = [
'title' => t('Quant Metadata'),
'field' => [
'title' => t('Quant Metadata'),
'id' => 'quant_metadata',
],
];
// Add the metadata field to all relevant views types.
$types = [
'file_managed',
'node_field_data',
'node_revision',
'redirect',
'taxonomy_term_field_data',
'taxonomy_term_revision',
];
foreach ($types as $type) {
if (isset($data[$type])) {
$data[$type]['quant_metadata'] = $field;
}
}
}
/**
* Implements theme_preprocess_views_view_table().
*/
function quant_preprocess_views_view_table(&$variables) {
$view = $variables["view"];
if (str_starts_with($view->id(), 'quant_metadata_')) {
$rows = $variables['rows'];
foreach ($rows as $id => $row) {
// @fixme Show warning class when published status does not match.
// $variables['rows'][$id]['attributes']->addClass('messages');
// $variables['rows'][$id]['attributes']->addClass('messages--warning');
}
}
}