Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / content_moderation / content_moderation.post_update.php
1 <?php
2
3 /**
4  * @file
5  * Post update functions for the Content Moderation module.
6  */
7
8 use Drupal\Core\Site\Settings;
9 use Drupal\workflows\Entity\Workflow;
10
11 /**
12  * Synchronize moderation state default revisions with their host entities.
13  */
14 function content_moderation_post_update_update_cms_default_revisions(&$sandbox) {
15   // For every moderated entity, identify the default revision ID, track the
16   // corresponding "content_moderation_state" revision and save it as the new
17   // default revision, if needed.
18
19   // Initialize sandbox info.
20   $entity_type_id = &$sandbox['entity_type_id'];
21   if (!isset($entity_type_id)) {
22     $sandbox['bundles'] = [];
23     /** @var \Drupal\workflows\WorkflowInterface $workflow */
24     foreach (Workflow::loadMultipleByType('content_moderation') as $workflow) {
25       /** @var \Drupal\content_moderation\Plugin\WorkflowType\ContentModeration $plugin */
26       $plugin = $workflow->getTypePlugin();
27       foreach ($plugin->getEntityTypes() as $entity_type_id) {
28         $sandbox['entity_type_ids'][$entity_type_id] = $entity_type_id;
29         foreach ($plugin->getBundlesForEntityType($entity_type_id) as $bundle) {
30           $sandbox['bundles'][$entity_type_id][$bundle] = $bundle;
31         }
32       }
33     }
34     $sandbox['offset'] = 0;
35     $sandbox['limit'] = Settings::get('entity_update_batch_size', 50);
36     $sandbox['total'] = count($sandbox['entity_type_ids']);
37     $entity_type_id = array_shift($sandbox['entity_type_ids']);
38   }
39
40   // If there are no moderated bundles or we processed all of them, we are done.
41   $entity_type_manager = \Drupal::entityTypeManager();
42   /** @var \Drupal\Core\Entity\ContentEntityStorageInterface $content_moderation_state_storage */
43   $content_moderation_state_storage = $entity_type_manager->getStorage('content_moderation_state');
44   if (!$entity_type_id) {
45     $content_moderation_state_storage->resetCache();
46     $sandbox['#finished'] = 1;
47     return;
48   }
49
50   // Retrieve a batch of moderated entities to be processed.
51   $storage = $entity_type_manager->getStorage($entity_type_id);
52   $entity_type = $entity_type_manager->getDefinition($entity_type_id);
53   $query = $storage->getQuery()
54     ->accessCheck(FALSE)
55     ->sort($entity_type->getKey('id'))
56     ->range($sandbox['offset'], $sandbox['limit']);
57   $bundle_key = $entity_type->getKey('bundle');
58   if ($bundle_key && !empty($sandbox['bundles'][$entity_type_id])) {
59     $bundles = array_keys($sandbox['bundles'][$entity_type_id]);
60     $query->condition($bundle_key, $bundles, 'IN');
61   }
62   $entity_ids = $query->execute();
63
64   // Compute progress status and skip to the next entity type, if needed.
65   $sandbox['#finished'] = ($sandbox['total'] - count($sandbox['entity_type_ids']) - 1) / $sandbox['total'];
66   if (!$entity_ids) {
67     $sandbox['offset'] = 0;
68     $entity_type_id = array_shift($sandbox['entity_type_ids']) ?: FALSE;
69     return;
70   }
71
72   // Load the "content_moderation_state" revisions corresponding to the
73   // moderated entity default revisions.
74   $result = $content_moderation_state_storage->getQuery()
75     ->allRevisions()
76     ->condition('content_entity_type_id', $entity_type_id)
77     ->condition('content_entity_revision_id', array_keys($entity_ids), 'IN')
78     ->execute();
79   /** @var \Drupal\Core\Entity\ContentEntityInterface[] $revisions */
80   $revisions = $content_moderation_state_storage->loadMultipleRevisions(array_keys($result));
81
82   // Update "content_moderation_state" data.
83   foreach ($revisions as $revision) {
84     if (!$revision->isDefaultRevision()) {
85       $revision->setNewRevision(FALSE);
86       $revision->isDefaultRevision(TRUE);
87       $content_moderation_state_storage->save($revision);
88     }
89   }
90
91   // Clear static cache to avoid memory issues.
92   $storage->resetCache($entity_ids);
93
94   $sandbox['offset'] += $sandbox['limit'];
95 }