' . t('About') . ''; $output .= '

' . t('Sample Migrations') . '

'; 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); $messenger->addMessage( t('An error occurred while processing @operation with arguments : @args', [ '@operation' => $error_operation[0], '@args' => print_r($error_operation[0], TRUE) ] ) ); } } /** * Undocumented function * * @return void */ function sample_migration_get_title_body_field_collections() { Database::setActiveConnection('drupal7'); $drupal7db = Database::getConnection(); $query = $drupal7db->select('field_data_field_test_paragraphs_content', 'fc'); $query->fields('fc', [ 'entity_id', 'field_test_paragraphs_content_value', 'delta' ]); $query->innerJoin('field_collection_item', 'fci', 'fc.entity_id = fci.item_id'); $query->fields('fci', [ 'item_id', 'field_name', 'revision_id' ]); $query->innerJoin('field_data_field_fc_title', 'title', 'title.entity_id = fci.item_id'); $query->fields('title', [ 'entity_type', 'bundle', 'entity_id', 'field_fc_title_value' ]); $query->innerJoin('field_data_field_fc_body', 'body', 'body.entity_id = fci.item_id'); $query->fields('body', [ 'entity_type', 'field_fc_body_value', 'field_fc_body_format', 'bundle', 'entity_id' ]); $results = $query->execute()->fetchAll(); Database::setActiveConnection('default'); 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'); $query->fields('mm', [ 'sourceid1', 'destid1' ]); $map_results = $query->execute()->fetchAllAssoc('sourceid1'); 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 ]); if (!is_null($existing_paragraph) && count($existing_paragraph)) { $this_paragraph = reset($existing_paragraph); } else { $this_paragraph = $paragraph_storage->create($paragraph_data); $this_paragraph->save(); } // attach paragraph to node $parent_node = $node_storage->load($current_result->entity_id); if (!is_null($parent_node)) { // parent field name $field_name = 'field_content'; // find all paragraphs already on the node so we don't add this one twice // also so we don't delete a paragraph by adding a new one $node_paragraphs = $parent_node->get($field_name)->getValue(); $should_add_paragraph = true; if (count($node_paragraphs)) { foreach ($node_paragraphs as $node_paragraph) { if ($node_paragraph['target_id'] === $this_paragraph->id()) { // if this paragraph is alrady attached, there is no reason to attach it again $should_add_paragraph = false; break; } } } if ($should_add_paragraph) { // add this paragraph to the paragraphs array on the node and save it $node_paragraphs[] = [ 'target_id' => $this_paragraph->id(), 'target_revision_id' => $this_paragraph->getRevisionId() ]; $parent_node->set($field_name, $node_paragraphs); $parent_node->save(); } } else { \Drupal::messenger()->addMessage(t("Unable to attach paragraph to content. Paragraph ID: " . $this_paragraph->id()), 'error'); \Drupal::logger('joco_migrations')->error("Unable to attach paragraph to content. Paragraph ID: " . $this_paragraph->id()); } }