You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

155 lines
4.7 KiB

4 years ago
  1. <?php
  2. /**
  3. * @file
  4. * Contains sample_migration.module.
  5. */
  6. use Drupal\Core\Routing\RouteMatchInterface;
  7. /**
  8. * Implements hook_help().
  9. */
  10. function sample_migration_help($route_name, RouteMatchInterface $route_match) {
  11. switch ($route_name) {
  12. // Main module help for the sample_migration module.
  13. case 'help.page.sample_migration':
  14. $output = '';
  15. $output .= '<h3>' . t('About') . '</h3>';
  16. $output .= '<p>' . t('Sample Migrations') . '</p>';
  17. return $output;
  18. default:
  19. }
  20. }
  21. function sample_migration_title_body_paragraphs(&$context) {
  22. $entity_type_manager = \Drupal::service('entity_type.manager');
  23. /* @var NodeStorage $node_storage */
  24. $node_storage = $entity_type_manager->getStorage('node');
  25. $media_storage = $entity_type_manager->getStorage('media');
  26. $file_storage = $entity_type_manager->getStorage('file');
  27. if (empty($context['sandbox'])) {
  28. $context['sandbox'] = [];
  29. }
  30. if (!isset($context['sandbox']['progress'])) {
  31. $context['sandbox']['progress'] = 0;
  32. $context['sandbox']['current_index'] = 0;
  33. $all_arm_pages = $node_storage->loadByProperties([
  34. 'type' => 'arm_page'
  35. ]);
  36. $context['sandbox']['arm_content'] = array_values($all_arm_pages);
  37. $context['sandbox']['arm_map'] = _get_arm_map();
  38. $context['sandbox']['file_map'] = _get_file_map();
  39. $context['sandbox']['d7_files'] = _get_d7_arm_files();
  40. $context['sandbox']['index'] = array_values($context['sandbox']['arm_content']);
  41. $context['sandbox']['max'] = count($context['sandbox']['arm_content']);
  42. }
  43. $limit = 10;
  44. $indexes = range($context['sandbox']['current_index'], $context['sandbox']['current_index'] + $limit);
  45. foreach ($indexes as $index) {
  46. $node = $context['sandbox']['arm_content'][$index];
  47. if (!is_null($node)) {
  48. $context['results'][] = $node->title->value;
  49. $d7nid = $context['sandbox']['arm_map'][$node->id()];
  50. $d7file = $context['sandbox']['d7_files'][$d7nid->sourceid1];
  51. if ($d7file) {
  52. $d8file = $context['sandbox']['file_map'][$d7file->field_armpage_pdf_fid];
  53. $current_file = $file_storage->load($d8file->destid1);
  54. if ($d8file) {
  55. $node->set('field_armpage_pdf', $current_file->id());
  56. $node->save();
  57. }
  58. }
  59. }
  60. $context['sandbox']['progress']++;
  61. $context['sandbox']['current_index'] = $index;
  62. $context['message'] = t('Now processing Reviews');
  63. }
  64. // Update batch on our progress.
  65. if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
  66. $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
  67. }
  68. else {
  69. $context['sandbox']['finished'] = 1;
  70. }
  71. }
  72. function sample_migration_title_body_paragraphs_finished() {
  73. $messenger = \Drupal::messenger();
  74. if ($success) {
  75. $message = t('@count Reviews were added to Groups', ['@count' => count($results)]);
  76. $messenger->addMessage($message);
  77. }
  78. else {
  79. // Something went wrong
  80. $error_operation = reset($operations);
  81. $messenger->addMessage(
  82. t('An error occurred while processing @operation with arguments : @args',
  83. [
  84. '@operation' => $error_operation[0],
  85. '@args' => print_r($error_operation[0], TRUE)
  86. ]
  87. )
  88. );
  89. }
  90. }
  91. function sample_migration_get_title_body_field_collections() {
  92. Database::setActiveConnection('drupal7');
  93. $drupal7db = Database::getConnection();
  94. $query = $drupal7db->select('field_collection_item', 'fci')
  95. ->fields('fci', [
  96. 'item_id',
  97. 'field_name',
  98. 'revision_id'
  99. ]);
  100. $query->innerJoin('field_data_' . $field_name, 'fd', 'fd.' . $field_name . '_value = fci.item_id');
  101. $query->fields('fd', [
  102. 'entity_type',
  103. 'bundle',
  104. 'entity_id',
  105. $field_name . '_revision_id'
  106. ]);
  107. $query->condition('fci.field_name', $field_name);
  108. $query->leftJoin('field_data_field_usingpage_block_image', 'fdfubi', 'fdfubi.entity_id = fci.item_id');
  109. $query->fields('fdfubi', [
  110. 'entity_id',
  111. 'revision_id',
  112. 'field_usingpage_block_image_fid'
  113. ]);
  114. $results = $query->execute()->fetchAll();
  115. Database::setActiveConnection('default');
  116. if (count($results)) {
  117. $drupal8db = Database::getConnection();
  118. $query = $drupal8db->select('migrate_map_upgrade_d7_node_using_library_page', 'mm');
  119. $query->fields('mm', [
  120. 'sourceid1',
  121. 'destid1'
  122. ]);
  123. $map_results = $query->execute()->fetchAllAssoc('sourceid1');
  124. }
  125. }
  126. function sample_migration_get_test_paragraph_content_map() {
  127. $drupal8db = Database::getConnection();
  128. $query = $drupal8db->select('migrate_map_upgrade_d7_node_using_library_page', 'mm');
  129. $query->fields('mm', [
  130. 'sourceid1',
  131. 'destid1'
  132. ]);
  133. $map_results = $query->execute()->fetchAllAssoc('sourceid1');
  134. }