-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiversion.module
717 lines (665 loc) · 26.4 KB
/
multiversion.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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
<?php
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Utility\Error;
use Drupal\multiversion\Entity\Storage\ContentEntityStorageInterface;
use Drupal\multiversion\Entity\WorkspaceInterface;
use Drupal\multiversion\MultiversionFieldItemList;
use Drupal\views\Plugin\views\query\QueryPluginBase;
use Drupal\views\ViewExecutable;
use Drupal\Core\Entity\EntityInterface;
use Drupal\multiversion\Entity\Workspace;
/**
* Implements hook_module_implements_alter().
*/
function multiversion_module_implements_alter(&$implementations, $hook) {
if ($hook == 'entity_type_alter') {
$group = $implementations['multiversion'];
unset($implementations['multiversion']);
$implementations = ['multiversion' => $group] + $implementations;
}
if ($hook == 'field_info_alter') {
$group = $implementations['multiversion'];
unset($implementations['multiversion']);
$implementations['multiversion'] = $group;
}
}
/**
* Implements hook_entity_type_alter().
*
* @param \Drupal\Core\Entity\EntityTypeInterface[] $entity_types
*/
function multiversion_entity_type_alter(array &$entity_types) {
/** @var \Drupal\multiversion\MultiversionManagerInterface $manager */
$manager = \Drupal::service('multiversion.manager');
foreach ($entity_types as $entity_type) {
if ($manager->allowToAlter($entity_type)) {
// Make all content entity types revisionable.
if (!$entity_type->isRevisionable()) {
// We only need to set the revision key to make an entity type
// revisionable. The table names will be handled by the storage class.
// @see \Drupal\Core\Entity\Sql\SqlContentEntityStorage::initTableLayout
$keys = $entity_type->getKeys();
$keys['revision'] = 'revision_id';
$entity_type->set('entity_keys', $keys);
if ($entity_type->getRevisionTable() === null) {
$entity_type->set('revision_table', $entity_type->id() . '_revision');
}
if ($entity_type->getRevisionDataTable() === null) {
$entity_type->set('revision_data_table', $entity_type->id() . '_field_revision');
}
}
$namespace = 'Drupal\multiversion\Entity\Storage\Sql';
$original_storage_class = $entity_type->getHandlerClass('storage');
$entity_type->setHandlerClass('original_storage', $original_storage_class);
switch ($entity_type->id()) {
case 'node':
$entity_type->setHandlerClass('storage', "$namespace\\NodeStorage");
break;
case 'taxonomy_term':
$entity_type->setHandlerClass('storage', "$namespace\\TermStorage");
break;
case 'comment':
$entity_type->setHandlerClass('storage', "$namespace\\CommentStorage");
break;
case 'menu_link_content':
$entity_type->setClass('Drupal\multiversion\Entity\MenuLinkContent');
$entity_type->setHandlerClass('storage', "$namespace\\MenuLinkContentStorage");
break;
case 'file':
$entity_type->setHandlerClass('storage', "$namespace\\FileStorage");
break;
case 'media':
$entity_type->setHandlerClass('storage', "$namespace\\MediaStorage");
break;
case 'paragraph':
$entity_type->setClass('Drupal\multiversion\Entity\Paragraph');
$entity_type->setHandlerClass('storage', "$namespace\\ContentEntityStorage");
break;
case 'poll':
$entity_type->setHandlerClass('storage', "$namespace\\PollStorage");
break;
case 'crop':
$entity_type->setHandlerClass('storage', "$namespace\\CropStorage");
break;
case 'redirect':
$entity_type->setHandlerClass('storage_schema', 'Drupal\multiversion\Redirect\RedirectStorageSchema');
if (in_array($original_storage_class, [NULL, 'Drupal\Core\Entity\Sql\SqlContentEntityStorage'])) {
$entity_type->setHandlerClass('storage', "$namespace\\ContentEntityStorage");
}
break;
default:
// We can only override the storage handler for entity types we know
// what to expect of.
if (in_array($original_storage_class, [NULL, 'Drupal\Core\Entity\Sql\SqlContentEntityStorage'])) {
$entity_type->setHandlerClass('storage', "$namespace\\ContentEntityStorage");
}
break;
}
}
}
if (isset($entity_types['block_content']) && $manager->allowToAlter($entity_types['block_content'])) {
$entity_types['block']->setHandlerClass('storage', 'Drupal\multiversion\Entity\Storage\Sql\BlockStorage');
}
}
/**
* Implements hook_entity_base_field_info().
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* @return \Drupal\Core\Field\BaseFieldDefinition[]
*/
function multiversion_entity_base_field_info(EntityTypeInterface $entity_type) {
/** @var \Drupal\multiversion\MultiversionManagerInterface $manager */
$manager = \Drupal::service('multiversion.manager');
if ($manager->allowToAlter($entity_type)) {
$fields = [];
// Get the minor version only from the \Drupal::VERSION string.
$minor_version = substr(\Drupal::VERSION, 0, 3);
// @todo: Alter the entity label field to make it revisionable.
// In some scenarios where's in a state of limbo where we've already
// altered and enabled the entity type but we're given an old entity type
// definition for this hook and we get an empty revision key. However,
// these are always the entity types that Multiversion has enabled revisions
// on, so we can assume the same name of the revision key.
$revision_key = $entity_type->getKey('revision') ?: 'revision_id';
// This will essentially overwrite the revision field definition but also
// ensure that entity types that we enabled revisions for get a revision
// field definition of a type that we expect.
$fields[$revision_key] = BaseFieldDefinition::create('integer')
->setLabel(t('Revision ID'))
->setDescription(t('The local revision ID of the entity.'))
->setReadOnly(TRUE)
->setSetting('unsigned', TRUE);
// Add the revision_default field on 8.5 or higher.
if (version_compare($minor_version, '8.5', '>=')) {
$fields['revision_default'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Default revision'))
->setDescription(t('A flag indicating whether this was a default revision when it was saved.'))
->setStorageRequired(TRUE)
->setTranslatable(FALSE)
->setRevisionable(TRUE)
// We cannot tell whether existing revisions were default or not when
// they were created, but since we did not support creating non-default
// revisions in any core stable UI so far, we default to TRUE.
->setInitialValue(TRUE);
}
// This field shouldn't really be revisionable since all revisions for an
// entity will only ever exist in one and the same workspace. But we mark
// this as revisionable to make the storage query more performance because
// then we don't need to join the data table (which it isn't by default).
if ($entity_type->get('workspace') !== FALSE) {
$fields['workspace'] = BaseFieldDefinition::create('workspace_reference')
->setLabel(t('Workspace reference'))
->setDescription(t('The workspace this entity belongs to.'))
->setSetting('target_type', 'workspace')
->setRevisionable(FALSE)
->setTranslatable(FALSE)
->setCardinality(1)
->setReadOnly(TRUE);
}
$fields['_deleted'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Deleted flag'))
->setDescription(t('Indicates if the entity is flagged as deleted or not.'))
->setRevisionable(TRUE)
->setTranslatable(FALSE)
->setDefaultValue(FALSE)
->setCardinality(1);
$fields['_rev'] = BaseFieldDefinition::create('revision_token')
->setLabel(t('Revision token'))
->setDescription(t('The token for this entity revision.'))
->setRevisionable(TRUE)
->setTranslatable(FALSE)
->setCardinality(1)
->setReadOnly(TRUE);
// Add the 'revision_translation_affected' field if needed. Limit this to
// Drupal version 8.4 and higher.
if (version_compare($minor_version, '8.4', '>=') && $entity_type->isTranslatable()) {
$fields[$entity_type->getKey('revision_translation_affected')] = BaseFieldDefinition::create('boolean')
->setName($entity_type->getKey('revision_translation_affected'))
->setTargetEntityTypeId($entity_type->id())
->setTargetBundle(NULL)
->setLabel(new TranslatableMarkup('Revision translation affected'))
->setDescription(new TranslatableMarkup('Indicates if the last edit of a translation belongs to current revision.'))
->setReadOnly(TRUE)
->setRevisionable(TRUE)
->setTranslatable(TRUE);
}
return $fields;
}
}
/**
* Implements hook_data_type_info_alter().
*/
function multiversion_data_type_info_alter(&$info) {
$info['entity_reference']['class'] = '\Drupal\multiversion\EntityReference';
}
/**
* Implements hook_field_info_alter().
*/
function multiversion_field_info_alter(&$info) {
$info['uuid']['class'] = '\Drupal\multiversion\Field\UuidItem';
$info['entity_reference']['class'] = '\Drupal\multiversion\EntityReferenceItem';
$info['file']['class'] = '\Drupal\multiversion\FileItem';
$info['image']['class'] = '\Drupal\multiversion\ImageItem';
if (isset($info['entity_reference_revisions'])) {
$info['entity_reference_revisions']['class'] = '\Drupal\multiversion\EntityReferenceRevisionsItem';
}
if (\Drupal::moduleHandler()->moduleExists('pathauto')) {
$info['path']['list_class'] = MultiversionFieldItemList::class;
}
}
/**
* Implements hook_entity_base_field_info_alter().
*
* @param array $fields
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
*/
function multiversion_entity_base_field_info_alter(&$fields, EntityTypeInterface $entity_type) {
/** @var \Drupal\multiversion\MultiversionManagerInterface $manager */
$manager = \Drupal::service('multiversion.manager');
if ($manager->allowToAlter($entity_type)) {
$exclude_fields = [
$entity_type->getKey('id'),
$entity_type->getKey('revision'),
$entity_type->getKey('uuid'),
$entity_type->getKey('bundle'),
$entity_type->getKey('langcode'),
'workspace',
'_deleted',
'_rev',
];
if ($entity_type->id() == 'comment') {
$exclude_fields[] = 'comment_type';
}
foreach ($fields as $key => $field) {
if (!in_array($key, $exclude_fields)) {
$field->setRevisionable(TRUE);
}
}
}
}
/**
* Implements hook_ENTITY_TYPE_update().
*/
function multiversion_workspace_update(EntityInterface $entity) {
/** @var \Drupal\multiversion\Entity\WorkspaceInterface $entity */
if (!$entity->isPublished() && $entity->original->isPublished()) {
$default_workspace = \Drupal::getContainer()->getParameter('workspace.default');
\Drupal::service('workspace.manager')->setActiveWorkspace(Workspace::load($default_workspace));
}
}
/**
* Implements hook_ENTITY_TYPE_presave().
*/
function multiversion_workspace_presave(EntityInterface $entity) {
if ($entity->isDefaultWorkspace() && !$entity->isPublished() && $entity->original->isPublished()) {
throw new Exception('The default workspace cannot be archived.');
}
}
/**
* Implements hook_ENTITY_TYPE_update().
*/
function multiversion_paragraph_update(EntityInterface $entity) {
// Update the target_revision_id field value for the parent entity if it
// changed (for example when saving a revision after a stub revision during
// replication).
$parent = $entity->getParentEntity();
$entity_revision_parent_field_name = $entity->getEntityType()->get('entity_revision_parent_field_name_field');
if ($parent instanceof ContentEntityInterface &&
!empty($entity->workspace) &&
$entity_revision_parent_field_name &&
!empty($entity->{$entity_revision_parent_field_name}->value) &&
(
$entity->_rev->is_stub ||
(!empty($entity->original) && $entity->original->_rev->is_stub)
)
) {
$parent_field_name = $entity->{$entity_revision_parent_field_name}->value;
$storage = \Drupal::entityTypeManager()->getStorage($parent->getEntityTypeId());
$storage->resetCache([$parent->id()]);
// Load the most recent version of the parent.
$parent = $storage->load($parent->id());
if ($parent && !empty($parent->{$parent_field_name})) {
$save_parent = FALSE;
$values = $parent->{$parent_field_name}->getValue();
$updated_values = [];
foreach ($values as $delta => $value) {
$updated_values[$delta] = $value;
if ($value['target_id'] == $entity->id() && $value['target_revision_id'] != $entity->getRevisionId()) {
$updated_values[$delta]['target_revision_id'] = $entity->getRevisionId();
$save_parent = TRUE;
}
}
if ($save_parent) {
$parent->{$parent_field_name}->setValue($updated_values);
try {
$storage->saveWithoutForcingNewRevision($parent);
$storage->resetCache([$parent->id()]);
}
catch (\Exception $e) {
$details = t('Failed to save parent entity with UUID: %uuid_parent for paragraph entity with UUID: %uuid_paragraph.',
[
'%uuid_parent' => $parent->uuid(),
'%uuid_paragraph' => $entity->uuid(),
]);
\Drupal::logger('Multiversion')->error('%type: @message in %function (line %line of %file). ' . $details, Error::decodeException($e));
}
}
}
}
}
/**
* Implements hook_views_data_alter().
*/
function multiversion_views_data_alter(array &$data) {
foreach ($data as $key => $item) {
// Set standard handler for _rev field.
if (isset($data[$key]['_rev'])) {
$data[$key]['_rev']['field']['id'] = 'standard';
}
if (isset($data[$key]['_deleted'])) {
// Use status = 1 instead of status <> 0 in WHERE statement.
$data[$key]['_deleted']['filter']['use_equal'] = TRUE;
}
// Add a new filter that filters content by current active workspace.
if (isset($data[$key]['workspace'])) {
$data[$key]['current_workspace'] = [
'title' => t('Current workspace'),
'help' => t('Filters content by current active workspace.'),
'filter' => [
'field' => 'workspace',
'id' => 'current_workspace',
'label' => t('Current workspace'),
],
];
// Unset the 'Workspace reference' filter because users are not allowed to
// filter by a specific workspace, other than current active workspace.
// To filter by current active workspace will be used the 'Current workspace'
// filter.
unset($data[$key]['workspace']);
}
}
}
/**
* Implements hook_views_post_execute().
*/
function multiversion_views_post_execute(ViewExecutable $view) {
// Add deleted entities if we have rows for them.
// When we want to get deleted entities using the _deleted field, entities
// should be loaded with
// \Drupal::entityManager()->getTypeStorage($entity_type)->loadDeleted($id) or
// \Drupal::entityManager()->getTypeStorage($entity_type)->loadMultipleDeleted($ids),
// otherwise the _entity field in the view result rows will be null.
$base_field = $view->storage->get('base_field');
$table_info = $view->query->getEntityTableInfo();
$content_type_info = array_column($table_info, 'entity_type');
if (is_array($view->result) && $content_type = reset($content_type_info)) {
$manager = \Drupal::service('multiversion.manager');
$storage = \Drupal::entityTypeManager()->getStorage($content_type);
if ($storage instanceof ContentEntityStorageInterface && $manager->allowToAlter($storage->getEntityType())) {
$ids = [];
foreach ($view->result as $index => $row) {
if (empty($row->_entity) && !empty($row->{$base_field})) {
$ids[$index] = $row->{$base_field};
}
}
$entities = $storage->loadMultipleDeleted($ids);
foreach ($view->result as $index => $row) {
if (empty($row->_entity) && !empty($row->{$base_field}) && isset($entities[$row->{$base_field}])) {
$view->result[$index]->_entity = $entities[$row->{$base_field}];
}
// In all other cases unset rows that don't have a value for _entity key.
elseif (empty($row->_entity)) {
unset($view->result[$index]);
}
}
}
}
}
/**
* Implements hook_views_query_alter().
*
* @param \Drupal\views\ViewExecutable $view
* The view object about to be processed.
* @param QueryPluginBase $query
* The query plugin object for the query.
*/
function multiversion_views_query_alter(ViewExecutable $view, QueryPluginBase $query) {
// Add a new filter for default core views, it will filter deleted content.
$views_ids = [
'content',
'frontpage',
'comments_recent',
'content_recent',
'taxonomy_term',
'glossary',
'archive',
'block_content',
'poll_admin',
'poll_list',
];
if (in_array($view->id(), $views_ids)) {
/** @var \Drupal\multiversion\MultiversionManagerInterface $manager */
$manager = \Drupal::service('multiversion.manager');
$entity_type = $view->getBaseEntityType();
if (!$manager->isEnabledEntityType($entity_type)) {
return;
}
$base_table = $view->storage->get('base_table');
$view->query->where[1]['conditions'][] = [
'field' => $base_table . '._deleted',
'value' => FALSE,
'operator' => '=',
];
$view->query->where[1]['conditions'][] = [
'field' => $base_table . '.workspace',
'value' => multiversion_get_active_workspace_id(),
'operator' => '=',
];
}
}
/**
* Implements hook_element_info_alter().
*/
function multiversion_element_info_alter(array &$types) {
foreach ($types as &$type) {
if (!isset($type['#pre_render'])) {
$type['#pre_render'] = [];
}
$type['#pre_render'][] = 'multiversion_element_pre_render';
}
}
/**
* Element pre-render callback.
*/
function multiversion_element_pre_render($element) {
if (isset($element['#cache'])) {
if (!isset($element['#cache']['contexts'])) {
$element['#cache']['contexts'] = [];
}
$element['#cache']['contexts'] = Cache::mergeContexts(
$element['#cache']['contexts'], ['workspace']
);
}
return $element;
}
/**
* Callback for getting the active workspace ID.
*/
function multiversion_get_active_workspace_id() {
return \Drupal::service('multiversion.manager')->getActiveWorkspaceId();
}
/**
* URI callback for the workspace entity type.
*/
function multiversion_workspace_uri(WorkspaceInterface $entity) {
return $entity->id();
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function multiversion_form_node_type_edit_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state) {
// Users don't have the option to disable revisions when using Multiversion.
// @todo: {@link https://www.drupal.org/node/2597393 See if there's a way
// to just disable this particular option.}
unset($form['workflow']['options']['#options']['revision']);
}
/**
* Implements hook_form_alter().
*/
function multiversion_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
if (isset($form['revision']['#group']) && $form['revision']['#group'] == 'revision_information') {
// Users don't have the option to disable revisions when using Multiversion.
$form['revision']['#default_value'] = TRUE;
$form['revision']['#disabled'] = TRUE;
}
}
/**
* Prepares a file destination directory.
*
* If the directory doesn't exist it tries to create it, if the directory is not
* writable it tries to make it writable. In case it can't create the directory
* or make it writable, logs the error message and returns FALSE.
* When the directory exists and it is writable returns TRUE.
*
* @param string $destination
*
* @return bool
*/
function multiversion_prepare_file_destination($destination) {
$dirname = \Drupal::service('file_system')->dirname($destination);
return file_prepare_directory($dirname, FILE_MODIFY_PERMISSIONS | FILE_CREATE_DIRECTORY);
}
/**
* Implements hook_menu_links_discovered_alter().
*/
function multiversion_menu_links_discovered_alter(&$links) {
// Get all custom menu links and set links with the correct ID.
// The ID format now will be 'menu_link_content:ENTITY_UUID:ENTITY_ID' - we
// need to change it because we need new entry in the menu_tree table for the
// same link on different workspaces.
// The old ID format is 'menu_link_content:ENTITY_UUID'.
if (\Drupal::moduleHandler()->moduleExists('menu_link_content')) {
$storage = \Drupal::entityTypeManager()->getStorage('menu_link_content');
if (\Drupal::service('multiversion.manager')->isEnabledEntityType($storage->getEntityType())) {
$workspaces = Workspace::loadMultiple();
foreach ($workspaces as $workspace_id => $workspace) {
$storage->useWorkspace($workspace_id);
$menu_link_content_entities = $storage->loadMultiple();
$new_ids = [];
$links_to_purge = [];
/** @var \Drupal\menu_link_content\MenuLinkContentInterface $menu_link_content */
foreach ($menu_link_content_entities as $menu_link_content) {
// Unset links with old ID format.
$uuid = $menu_link_content->uuid();
$old_id = "menu_link_content:$uuid";
$new_id = "$old_id:" . $menu_link_content->id();
if (isset($links[$old_id])) {
$links_to_purge[] = $old_id;
unset($links[$old_id]);
}
$new_ids[$old_id] = $new_id;
if (!isset($links[$new_id])) {
$links[$new_id] = $menu_link_content->getPluginDefinition();
}
// Set a new plugin class tha will handle new ID format.
$links[$new_id]['class'] = 'Drupal\multiversion\Plugin\Menu\MenuLinkContent';
}
if ($links_to_purge) {
\Drupal::service('menu.tree_storage')->purgeMultiple($links_to_purge);
}
foreach ($links as $id => $link) {
if (!empty($link['parent']) && in_array($link['parent'], array_keys($new_ids))) {
$links[$id]['parent'] = $new_ids[$link['parent']];
}
}
$storage->useWorkspace(NULL);
}
}
}
}
/**
* Implements hook_modules_installed().
*/
function multiversion_modules_installed($modules) {
// Enable entity types provided by installed modules and supported by
// Multiversion.
$entity_type_manager = \Drupal::entityTypeManager();
$supported_entity_types = \Drupal::configFactory()
->getEditable('multiversion.settings')
->get('supported_entity_types');
$supported_entity_types = $supported_entity_types ?: [];
$entities_to_enable = [];
foreach ($supported_entity_types as $entity_type_id) {
$entity_type = $entity_type_manager->getDefinition($entity_type_id, FALSE);
if (!empty($entity_type) && in_array($entity_type->getProvider(), $modules)) {
$entities_to_enable[$entity_type_id] = $entity_type;
}
}
if (!empty($entities_to_enable)) {
\Drupal::service('multiversion.manager')->enableEntityTypes($entities_to_enable);
}
}
/**
* Implements hook_search_plugin_alter().
*/
function multiversion_search_plugin_alter(array &$definitions) {
if (isset($definitions['node_search'])) {
$definitions['node_search']['class'] = 'Drupal\multiversion\Entity\Search\NodeSearch';
}
}
/**
* Implements hook_preprocess_HOOK().
*
* Adds the workspace as a class to the body.
*/
function multiversion_preprocess_html(&$variables) {
$active_workspace = \Drupal::service('workspace.manager')
->getActiveWorkspace();
if ($active_workspace && $machine_name = $active_workspace->getMachineName()) {
// Add a new body class with the active workspace.
$variables['attributes']['class'][] = 'workspace-' . $machine_name;
}
}
/**
* Implements hook_form_FORM_ID_alter().
*
* Alters the 'revision_overview_form' for provided by Diff module.
*/
function multiversion_form_revision_overview_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if (isset($form['node_revisions_table'])) {
foreach ($form['node_revisions_table'] as $key => $revision_info) {
if (empty($revision_info['operations']) || !is_array($revision_info['operations'])) {
continue;
}
if (isset($revision_info['operations']['#links']['delete'])) {
unset($form['node_revisions_table'][$key]['operations']['#links']['delete']);
}
}
}
}
/**
* Add workspace field in url_alias table.
*
* @param bool $install
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
*/
function _multiversion_add_workspace_field_in_url_alias_table($install = FALSE) {
$database = \Drupal::database();
$schema = $database->schema();
$table = 'url_alias';
$field = 'workspace';
if (!$schema->fieldExists($table, $field)) {
$spec = [
'description' => 'The workspace the alias belongs to.',
'type' => 'int',
'unsigned' => TRUE,
'default' => $install ? 1 : 0,
'size' => 'normal',
];
$schema->addField($table, $field, $spec);
}
$table_data = $database->select($table)
->fields($table)
->execute()
->fetchAll();
$entity_type_manager = \Drupal::entityTypeManager();
$entity_type_manager->clearCachedDefinitions();
$entity_ids = [];
foreach ($table_data as $row) {
$row_data = (array) $row;
$source_elements = explode('/', $row_data['source']);
if (!empty($source_elements[1]) && !empty($source_elements[2]) && is_numeric($source_elements[2])) {
if ($entity_type_manager->getDefinition($source_elements[1], FALSE)) {
$entity_ids[$source_elements[1]][$row_data['pid']] = $source_elements[2];
}
}
}
$workspaces = Workspace::loadMultiple();
foreach ($workspaces as $workspace_id => $workspace) {
foreach ($entity_ids as $entity_type_id => $ids) {
$storage = $entity_type_manager->getStorage($entity_type_id);
if ($storage instanceof ContentEntityStorageInterface) {
$storage->useWorkspace($workspace_id);
$entities = $storage->loadMultiple($ids);
$storage->useWorkspace(NULL);
}
if (empty($entities)) {
continue;
}
foreach ($ids as $pid => $entity_id) {
if (in_array($entity_id, array_keys($entities))) {
$database->update($table)
->fields([$field => $workspace_id])
->condition('pid', $pid)
->execute();
}
}
}
}
}