content_moderation.post_update.php 3.7 KB

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