content_moderation.post_update.php 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. /** @var \Drupal\workflows\WorkflowInterface $workflow */
  20. foreach (Workflow::loadMultipleByType('content_moderation') as $workflow) {
  21. /** @var \Drupal\content_moderation\Plugin\WorkflowType\ContentModeration $plugin */
  22. $plugin = $workflow->getTypePlugin();
  23. foreach ($plugin->getEntityTypes() as $entity_type_id) {
  24. $sandbox['entity_type_ids'][$entity_type_id] = $entity_type_id;
  25. foreach ($plugin->getBundlesForEntityType($entity_type_id) as $bundle) {
  26. $sandbox['bundles'][$entity_type_id][$bundle] = $bundle;
  27. }
  28. }
  29. }
  30. $sandbox['offset'] = 0;
  31. $sandbox['limit'] = Settings::get('entity_update_batch_size', 50);
  32. $sandbox['total'] = count($sandbox['entity_type_ids']);
  33. $entity_type_id = array_shift($sandbox['entity_type_ids']);
  34. }
  35. // If there are no moderated bundles or we processed all of them, we are done.
  36. $entity_type_manager = \Drupal::entityTypeManager();
  37. /** @var \Drupal\Core\Entity\ContentEntityStorageInterface $content_moderation_state_storage */
  38. $content_moderation_state_storage = $entity_type_manager->getStorage('content_moderation_state');
  39. if (!$entity_type_id) {
  40. $content_moderation_state_storage->resetCache();
  41. $sandbox['#finished'] = 1;
  42. return;
  43. }
  44. // Retrieve a batch of moderated entities to be processed.
  45. $storage = $entity_type_manager->getStorage($entity_type_id);
  46. $entity_type = $entity_type_manager->getDefinition($entity_type_id);
  47. $query = $storage->getQuery()
  48. ->accessCheck(FALSE)
  49. ->sort($entity_type->getKey('id'))
  50. ->range($sandbox['offset'], $sandbox['limit']);
  51. $bundle_key = $entity_type->getKey('bundle');
  52. if ($bundle_key && !empty($sandbox['bundles'][$entity_type_id])) {
  53. $bundles = array_keys($sandbox['bundles'][$entity_type_id]);
  54. $query->condition($bundle_key, $bundles, 'IN');
  55. }
  56. $entity_ids = $query->execute();
  57. // Compute progress status and skip to the next entity type, if needed.
  58. $sandbox['#finished'] = ($sandbox['total'] - count($sandbox['entity_type_ids']) - 1) / $sandbox['total'];
  59. if (!$entity_ids) {
  60. $sandbox['offset'] = 0;
  61. $entity_type_id = array_shift($sandbox['entity_type_ids']) ?: FALSE;
  62. return;
  63. }
  64. // Load the "content_moderation_state" revisions corresponding to the
  65. // moderated entity default revisions.
  66. $result = $content_moderation_state_storage->getQuery()
  67. ->allRevisions()
  68. ->condition('content_entity_type_id', $entity_type_id)
  69. ->condition('content_entity_revision_id', array_keys($entity_ids), 'IN')
  70. ->execute();
  71. /** @var \Drupal\Core\Entity\ContentEntityInterface[] $revisions */
  72. $revisions = $content_moderation_state_storage->loadMultipleRevisions(array_keys($result));
  73. // Update "content_moderation_state" data.
  74. foreach ($revisions as $revision) {
  75. if (!$revision->isDefaultRevision()) {
  76. $revision->setNewRevision(FALSE);
  77. $revision->isDefaultRevision(TRUE);
  78. $content_moderation_state_storage->save($revision);
  79. }
  80. }
  81. // Clear static cache to avoid memory issues.
  82. $storage->resetCache($entity_ids);
  83. $sandbox['offset'] += $sandbox['limit'];
  84. }