diff --git a/README.md b/README.md index 04ebb70..a9acc94 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,6 @@ -# Composer template for Drupal projects -Code Koalas Edition +# Drupal 8 Migrations ## How to use -1. `composer create-project codekoalas/koality-drupal:8.x-dev some-dir --stability dev --no-interaction` -2. `fin up` -3. `cp sample.settings.php docroot/sites/default/settings.php` -4. Go to your new local site. +1. `fin up` -# Previous Readme -Since this is a fork the previous README can be viewed back on the main project: -https://github.com/drupal-composer/drupal-project diff --git a/docroot/modules/custom/sample_migration/sample_migration.module b/docroot/modules/custom/sample_migration/sample_migration.module index c872554..d44a1de 100644 --- a/docroot/modules/custom/sample_migration/sample_migration.module +++ b/docroot/modules/custom/sample_migration/sample_migration.module @@ -24,9 +24,17 @@ function sample_migration_help($route_name, RouteMatchInterface $route_match) { } } +/** + * 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'); @@ -37,9 +45,13 @@ function sample_migration_title_body_paragraphs(&$context) { 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(); @@ -51,30 +63,32 @@ function sample_migration_title_body_paragraphs(&$context) { $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)) { - $node = $node_storage->load($d8_nid); - if (!is_null($node)) { - $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); - } + // 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); } } } @@ -93,6 +107,14 @@ function sample_migration_title_body_paragraphs(&$context) { } } +/** + * 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) { @@ -113,6 +135,11 @@ function sample_migration_title_body_paragraphs_finished($success, $results, $op } } +/** + * Undocumented function + * + * @return void + */ function sample_migration_get_title_body_field_collections() { Database::setActiveConnection('drupal7'); $drupal7db = Database::getConnection(); @@ -149,6 +176,11 @@ function sample_migration_get_title_body_field_collections() { return $results; } +/** + * Undocumented function + * + * @return void + */ function sample_migration_get_test_paragraph_content_map() { $drupal8db = Database::getConnection(); $query = $drupal8db->select('migrate_map_d7_node__test_paragraphs', 'mm'); @@ -160,11 +192,19 @@ function sample_migration_get_test_paragraph_content_map() { return $map_results; } +/** + * Create / Update a paragraph based on the data passed into it + * + * @param [type] $current_result + * @param [type] $paragraph_data + * @return void + */ function sample_migration_create_title_body_paragraph($current_result, $paragraph_data) { $paragraph_storage = \Drupal::entityTypeManager()->getStorage('paragraph'); $node_storage = \Drupal::entityTypeManager()->getStorage('node'); // check if paragraph already exists, if so we need to update it + // we set field_drupal7_item_id on it so we can make sure we aren't duplicating paragraphs // so we don't build a million paragraphs $existing_paragraph = $paragraph_storage->loadByProperties([ 'field_drupal7_item_id' => $current_result->field_test_paragraphs_content_value @@ -180,7 +220,7 @@ function sample_migration_create_title_body_paragraph($current_result, $paragrap // attach paragraph to node $parent_node = $node_storage->load($current_result->entity_id); if (!is_null($parent_node)) { - // based on different parent node type the field we attach it to is differently + // parent field name $field_name = 'field_content'; // find all paragraphs already on the node so we don't add this one twice