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.
 
 

253 lines
8.3 KiB

<?php
/**
* @file
* Contains sample_migration.module.
*/
use Drupal\Core\Routing\RouteMatchInterface;
use \Drupal\Core\Database\Database;
/**
* Implements hook_help().
*/
function sample_migration_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the sample_migration module.
case 'help.page.sample_migration':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Sample Migrations') . '</p>';
return $output;
default:
}
}
/**
* Batch Process to migrate all Title Body Field Collections from Drupal 7
* into the Title Body Paragraphs in Drupal 8
* You can read more about Batches in Drupal 8 here:
* https://api.drupal.org/api/drupal/core%21includes%21form.inc/group/batch/8.5.x
*
* @param [type] $context
* @return void
*/
function sample_migration_title_body_paragraphs(&$context) {
$entity_type_manager = \Drupal::service('entity_type.manager');
/* @var NodeStorage $node_storage */
$node_storage = $entity_type_manager->getStorage('node');
if (empty($context['sandbox'])) {
$context['sandbox'] = [];
}
if (!isset($context['sandbox']['progress'])) {
$context['sandbox']['progress'] = 0;
$context['sandbox']['current_index'] = 0;
// Load all Current Nodes type of Page
$all_pages = $node_storage->loadByProperties([
'type' => 'page'
]);
// Need to set a bunch of data on the sandbox key to keep it on every loop through the batch process
$context['sandbox']['page_content'] = array_values($all_pages);
$context['sandbox']['title_page_fc'] = sample_migration_get_title_body_field_collections();
$context['sandbox']['test_paragraph_map'] = sample_migration_get_test_paragraph_content_map();
$context['sandbox']['index'] = array_values($context['sandbox']['page_content']);
$context['sandbox']['max'] = count($context['sandbox']['page_content']);
}
$limit = 10;
$indexes = range($context['sandbox']['current_index'], $context['sandbox']['current_index'] + $limit);
// here is each loop through the batch process
foreach ($indexes as $index) {
// based on index get the current field collection item I'm on
// use a bunch of ifs to make sure we can proceed without breaking everything
$current_result = $context['sandbox']['title_page_fc'][$index];
if (!is_null($current_result)) {
$d7_nid = $current_result->entity_id;
if (!is_null($context['sandbox']['title_page_fc'][$index])) {
$d8_nid = $context['sandbox']['test_paragraph_map'][$d7_nid]->destid1;
if (!is_null($d8_nid)) {
// if we made it this far it's safe to try and create a paragraph
// Once the paragraph data is setup pass it to our create paragraph function
$paragraph_data = [
'field_title' => $current_result->field_fc_title_value,
'field_body' => [
'value' => $current_result->field_fc_body_value,
'format' => 'full_html',
],
'field_drupal7_item_id' => $current_result->field_test_paragraphs_content_value,
'parent_type' => 'node',
'type' => 'title_body',
'status' => '1',
'parent_field_name' => 'field_content',
'parent_id' => $d8_nid
];
sample_migration_create_title_body_paragraph($current_result, $paragraph_data);
}
}
}
$context['sandbox']['progress']++;
$context['sandbox']['current_index'] = $index;
$context['message'] = t('Now processing Paragraphs');
}
// Update batch on our progress.
if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
$context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
}
else {
$context['sandbox']['finished'] = 1;
}
}
/**
* Simple batch process finished function
*
* @param [type] $success
* @param [type] $results
* @param [type] $operations
* @return void
*/
function sample_migration_title_body_paragraphs_finished($success, $results, $operations) {
$messenger = \Drupal::messenger();
if ($success) {
$message = t('@count Reviews were added to Groups', ['@count' => count($results)]);
$messenger->addMessage($message);
}
else {
// Something went wrong
$error_operation = reset($operations);