diff --git a/.docksal/docksal.env b/.docksal/docksal.env index a30d277..a35febc 100644 --- a/.docksal/docksal.env +++ b/.docksal/docksal.env @@ -1,6 +1,6 @@ DOCKSAL_STACK=default DOCROOT=docroot -XDEBUG_ENABLED=0 +XDEBUG_ENABLED=1 DATABASE_NAME=default DATABASE_USERNAME=user DATABASE_PASSWORD=user diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..1e2d55a --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,28 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Listen for XDebug", + "type": "php", + "request": "launch", + "port": 9000, + "pathMappings": { + "/var/www/": "${workspaceRoot}" + } + }, + { + "name": "Launch currently open script", + "type": "php", + "request": "launch", + "program": "${file}", + "cwd": "${fileDirname}", + "port": 9000, + "pathMappings": { + "/var/www/": "${workspaceRoot}" + } + } + ] +} diff --git a/config/default/default/core.entity_form_display.node.should_have_been_page.default.yml b/config/default/default/core.entity_form_display.node.should_have_been_page.default.yml new file mode 100644 index 0000000..a5bb641 --- /dev/null +++ b/config/default/default/core.entity_form_display.node.should_have_been_page.default.yml @@ -0,0 +1,77 @@ +uuid: 421c9fe9-7f0e-40cf-bd03-5473ed8727be +langcode: en +status: true +dependencies: + config: + - field.field.node.should_have_been_page.body + - node.type.should_have_been_page + module: + - path + - text +id: node.should_have_been_page.default +targetEntityType: node +bundle: should_have_been_page +mode: default +content: + body: + type: text_textarea_with_summary + weight: 121 + settings: + rows: 9 + summary_rows: 3 + placeholder: '' + show_summary: false + third_party_settings: { } + region: content + created: + type: datetime_timestamp + weight: 10 + region: content + settings: { } + third_party_settings: { } + path: + type: path + weight: 30 + region: content + settings: { } + third_party_settings: { } + promote: + type: boolean_checkbox + settings: + display_label: true + weight: 15 + region: content + third_party_settings: { } + status: + type: boolean_checkbox + settings: + display_label: true + weight: 120 + region: content + third_party_settings: { } + sticky: + type: boolean_checkbox + settings: + display_label: true + weight: 16 + region: content + third_party_settings: { } + title: + type: string_textfield + weight: -5 + region: content + settings: + size: 60 + placeholder: '' + third_party_settings: { } + uid: + type: entity_reference_autocomplete + weight: 5 + settings: + match_operator: CONTAINS + size: 60 + placeholder: '' + match_limit: 10 + region: content + third_party_settings: { } +hidden: { } diff --git a/config/default/default/core.entity_form_display.node.test_paragraphs.default.yml b/config/default/default/core.entity_form_display.node.test_paragraphs.default.yml new file mode 100644 index 0000000..c0d9de8 --- /dev/null +++ b/config/default/default/core.entity_form_display.node.test_paragraphs.default.yml @@ -0,0 +1,79 @@ +uuid: 0b3363a7-1856-41e2-8d60-baff967b4079 +langcode: en +status: true +dependencies: + config: + - field.field.node.test_paragraphs.field_content + - node.type.test_paragraphs + module: + - paragraphs + - path +id: node.test_paragraphs.default +targetEntityType: node +bundle: test_paragraphs +mode: default +content: + created: + type: datetime_timestamp + weight: 10 + region: content + settings: { } + third_party_settings: { } + field_content: + type: entity_reference_paragraphs + weight: 121 + settings: + title: Paragraph + title_plural: Paragraphs + edit_mode: open + add_mode: dropdown + form_display_mode: default + default_paragraph_type: '' + third_party_settings: { } + region: content + path: + type: path + weight: 30 + region: content + settings: { } + third_party_settings: { } + promote: + type: boolean_checkbox + settings: + display_label: true + weight: 15 + region: content + third_party_settings: { } + status: + type: boolean_checkbox + settings: + display_label: true + weight: 120 + region: content + third_party_settings: { } + sticky: + type: boolean_checkbox + settings: + display_label: true + weight: 16 + region: content + third_party_settings: { } + title: + type: string_textfield + weight: -5 + region: content + settings: + size: 60 + placeholder: '' + third_party_settings: { } + uid: + type: entity_reference_autocomplete + weight: 5 + settings: + match_operator: CONTAINS + size: 60 + placeholder: '' + match_limit: 10 + region: content + third_party_settings: { } +hidden: { } diff --git a/config/default/default/core.entity_form_display.paragraph.title_body.default.yml b/config/default/default/core.entity_form_display.paragraph.title_body.default.yml new file mode 100644 index 0000000..bf1e938 --- /dev/null +++ b/config/default/default/core.entity_form_display.paragraph.title_body.default.yml @@ -0,0 +1,43 @@ +uuid: 9a87b01b-0d59-4250-8a18-1711ec8e4130 +langcode: en +status: true +dependencies: + config: + - field.field.paragraph.title_body.field_body + - field.field.paragraph.title_body.field_drupal7_item_id + - field.field.paragraph.title_body.field_title + - paragraphs.paragraphs_type.title_body + module: + - text +id: paragraph.title_body.default +targetEntityType: paragraph +bundle: title_body +mode: default +content: + field_body: + weight: 1 + settings: + rows: 5 + placeholder: '' + third_party_settings: { } + type: text_textarea + region: content + field_drupal7_item_id: + weight: 2 + settings: + size: 60 + placeholder: '' + third_party_settings: { } + type: string_textfield + region: content + field_title: + weight: 0 + settings: + size: 60 + placeholder: '' + third_party_settings: { } + type: string_textfield + region: content +hidden: + created: true + status: true diff --git a/config/default/default/core.entity_view_display.node.should_have_been_page.default.yml b/config/default/default/core.entity_view_display.node.should_have_been_page.default.yml new file mode 100644 index 0000000..f668035 --- /dev/null +++ b/config/default/default/core.entity_view_display.node.should_have_been_page.default.yml @@ -0,0 +1,28 @@ +uuid: ee6af7b5-0abf-4c69-a409-0c46acd5bc08 +langcode: en +status: true +dependencies: + config: + - field.field.node.should_have_been_page.body + - node.type.should_have_been_page + module: + - text + - user +id: node.should_have_been_page.default +targetEntityType: node +bundle: should_have_been_page +mode: default +content: + body: + label: hidden + type: text_default + weight: 101 + settings: { } + third_party_settings: { } + region: content + links: + weight: 100 + settings: { } + third_party_settings: { } + region: content +hidden: { } diff --git a/config/default/default/core.entity_view_display.node.should_have_been_page.teaser.yml b/config/default/default/core.entity_view_display.node.should_have_been_page.teaser.yml new file mode 100644 index 0000000..636988c --- /dev/null +++ b/config/default/default/core.entity_view_display.node.should_have_been_page.teaser.yml @@ -0,0 +1,30 @@ +uuid: 93ee5bde-27ee-45c8-a75f-c0bf69d191f4 +langcode: en +status: true +dependencies: + config: + - core.entity_view_mode.node.teaser + - field.field.node.should_have_been_page.body + - node.type.should_have_been_page + module: + - text + - user +id: node.should_have_been_page.teaser +targetEntityType: node +bundle: should_have_been_page +mode: teaser +content: + body: + label: hidden + type: text_summary_or_trimmed + weight: 101 + settings: + trim_length: 600 + third_party_settings: { } + region: content + links: + weight: 100 + settings: { } + third_party_settings: { } + region: content +hidden: { } diff --git a/config/default/default/core.entity_view_display.node.test_paragraphs.default.yml b/config/default/default/core.entity_view_display.node.test_paragraphs.default.yml new file mode 100644 index 0000000..29500df --- /dev/null +++ b/config/default/default/core.entity_view_display.node.test_paragraphs.default.yml @@ -0,0 +1,30 @@ +uuid: 37a2394f-5cce-4d5c-8938-0aa8012bb98e +langcode: en +status: true +dependencies: + config: + - field.field.node.test_paragraphs.field_content + - node.type.test_paragraphs + module: + - entity_reference_revisions + - user +id: node.test_paragraphs.default +targetEntityType: node +bundle: test_paragraphs +mode: default +content: + field_content: + type: entity_reference_revisions_entity_view + weight: 101 + label: above + settings: + view_mode: default + link: '' + third_party_settings: { } + region: content + links: + weight: 100 + settings: { } + third_party_settings: { } + region: content +hidden: { } diff --git a/config/default/default/core.entity_view_display.paragraph.title_body.default.yml b/config/default/default/core.entity_view_display.paragraph.title_body.default.yml new file mode 100644 index 0000000..f559bc5 --- /dev/null +++ b/config/default/default/core.entity_view_display.paragraph.title_body.default.yml @@ -0,0 +1,40 @@ +uuid: fa30105a-148d-4d77-8904-4894c408bddf +langcode: en +status: true +dependencies: + config: + - field.field.paragraph.title_body.field_body + - field.field.paragraph.title_body.field_drupal7_item_id + - field.field.paragraph.title_body.field_title + - paragraphs.paragraphs_type.title_body + module: + - text +id: paragraph.title_body.default +targetEntityType: paragraph +bundle: title_body +mode: default +content: + field_body: + weight: 1 + label: above + settings: { } + third_party_settings: { } + type: text_default + region: content + field_drupal7_item_id: + weight: 2 + label: above + settings: + link_to_entity: false + third_party_settings: { } + type: string + region: content + field_title: + weight: 0 + label: above + settings: + link_to_entity: false + third_party_settings: { } + type: string + region: content +hidden: { } diff --git a/config/default/default/field.field.node.should_have_been_page.body.yml b/config/default/default/field.field.node.should_have_been_page.body.yml new file mode 100644 index 0000000..1f40088 --- /dev/null +++ b/config/default/default/field.field.node.should_have_been_page.body.yml @@ -0,0 +1,23 @@ +uuid: d6760524-c83c-4bb0-8dfb-a455099384f6 +langcode: en +status: true +dependencies: + config: + - field.storage.node.body + - node.type.should_have_been_page + module: + - text +id: node.should_have_been_page.body +field_name: body +entity_type: node +bundle: should_have_been_page +label: Body +description: '' +required: false +translatable: true +default_value: { } +default_value_callback: '' +settings: + display_summary: true + required_summary: false +field_type: text_with_summary diff --git a/config/default/default/field.field.node.test_paragraphs.field_content.yml b/config/default/default/field.field.node.test_paragraphs.field_content.yml new file mode 100644 index 0000000..ff01e6a --- /dev/null +++ b/config/default/default/field.field.node.test_paragraphs.field_content.yml @@ -0,0 +1,31 @@ +uuid: dda2557f-fc1e-4ba4-87b0-c9139f08fc51 +langcode: en +status: true +dependencies: + config: + - field.storage.node.field_content + - node.type.test_paragraphs + - paragraphs.paragraphs_type.title_body + module: + - entity_reference_revisions +id: node.test_paragraphs.field_content +field_name: field_content +entity_type: node +bundle: test_paragraphs +label: Content +description: '' +required: false +translatable: false +default_value: { } +default_value_callback: '' +settings: + handler: 'default:paragraph' + handler_settings: + negate: 0 + target_bundles: + title_body: title_body + target_bundles_drag_drop: + title_body: + enabled: true + weight: 2 +field_type: entity_reference_revisions diff --git a/config/default/default/field.field.paragraph.title_body.field_body.yml b/config/default/default/field.field.paragraph.title_body.field_body.yml new file mode 100644 index 0000000..2f18ca6 --- /dev/null +++ b/config/default/default/field.field.paragraph.title_body.field_body.yml @@ -0,0 +1,21 @@ +uuid: 9708c395-e2e0-4995-86f6-d25fcd7538cf +langcode: en +status: true +dependencies: + config: + - field.storage.paragraph.field_body + - paragraphs.paragraphs_type.title_body + module: + - text +id: paragraph.title_body.field_body +field_name: field_body +entity_type: paragraph +bundle: title_body +label: Body +description: '' +required: false +translatable: false +default_value: { } +default_value_callback: '' +settings: { } +field_type: text_long diff --git a/config/default/default/field.field.paragraph.title_body.field_drupal7_item_id.yml b/config/default/default/field.field.paragraph.title_body.field_drupal7_item_id.yml new file mode 100644 index 0000000..ba32e4f --- /dev/null +++ b/config/default/default/field.field.paragraph.title_body.field_drupal7_item_id.yml @@ -0,0 +1,19 @@ +uuid: e9172a33-7697-4f86-8c33-cbf259f03b64 +langcode: en +status: true +dependencies: + config: + - field.storage.paragraph.field_drupal7_item_id + - paragraphs.paragraphs_type.title_body +id: paragraph.title_body.field_drupal7_item_id +field_name: field_drupal7_item_id +entity_type: paragraph +bundle: title_body +label: drupal7_item_id +description: '' +required: false +translatable: false +default_value: { } +default_value_callback: '' +settings: { } +field_type: string diff --git a/config/default/default/field.field.paragraph.title_body.field_title.yml b/config/default/default/field.field.paragraph.title_body.field_title.yml new file mode 100644 index 0000000..391e25c --- /dev/null +++ b/config/default/default/field.field.paragraph.title_body.field_title.yml @@ -0,0 +1,19 @@ +uuid: c9acc485-3d60-4bb2-a4a1-c70e2bfcd9a4 +langcode: en +status: true +dependencies: + config: + - field.storage.paragraph.field_title + - paragraphs.paragraphs_type.title_body +id: paragraph.title_body.field_title +field_name: field_title +entity_type: paragraph +bundle: title_body +label: Title +description: '' +required: false +translatable: false +default_value: { } +default_value_callback: '' +settings: { } +field_type: string diff --git a/config/default/default/field.storage.node.field_content.yml b/config/default/default/field.storage.node.field_content.yml new file mode 100644 index 0000000..fd79a9a --- /dev/null +++ b/config/default/default/field.storage.node.field_content.yml @@ -0,0 +1,21 @@ +uuid: e13ad7d1-cf8b-4192-838f-566a738d5333 +langcode: en +status: true +dependencies: + module: + - entity_reference_revisions + - node + - paragraphs +id: node.field_content +field_name: field_content +entity_type: node +type: entity_reference_revisions +settings: + target_type: paragraph +module: entity_reference_revisions +locked: false +cardinality: -1 +translatable: true +indexes: { } +persist_with_no_fields: false +custom_storage: false diff --git a/config/default/default/field.storage.paragraph.field_body.yml b/config/default/default/field.storage.paragraph.field_body.yml new file mode 100644 index 0000000..456d8d0 --- /dev/null +++ b/config/default/default/field.storage.paragraph.field_body.yml @@ -0,0 +1,19 @@ +uuid: 19642806-ee05-4989-8e62-e45fd96e5e5e +langcode: en +status: true +dependencies: + module: + - paragraphs + - text +id: paragraph.field_body +field_name: field_body +entity_type: paragraph +type: text_long +settings: { } +module: text +locked: false +cardinality: 1 +translatable: true +indexes: { } +persist_with_no_fields: false +custom_storage: false diff --git a/config/default/default/field.storage.paragraph.field_drupal7_item_id.yml b/config/default/default/field.storage.paragraph.field_drupal7_item_id.yml new file mode 100644 index 0000000..c329371 --- /dev/null +++ b/config/default/default/field.storage.paragraph.field_drupal7_item_id.yml @@ -0,0 +1,21 @@ +uuid: f3f0c0c7-bccd-435b-b4ba-344b4225dd06 +langcode: en +status: true +dependencies: + module: + - paragraphs +id: paragraph.field_drupal7_item_id +field_name: field_drupal7_item_id +entity_type: paragraph +type: string +settings: + max_length: 255 + is_ascii: false + case_sensitive: false +module: core +locked: false +cardinality: 1 +translatable: true +indexes: { } +persist_with_no_fields: false +custom_storage: false diff --git a/config/default/default/field.storage.paragraph.field_title.yml b/config/default/default/field.storage.paragraph.field_title.yml new file mode 100644 index 0000000..557a6ab --- /dev/null +++ b/config/default/default/field.storage.paragraph.field_title.yml @@ -0,0 +1,21 @@ +uuid: 888d992a-867c-434f-b010-de4a162c5b40 +langcode: en +status: true +dependencies: + module: + - paragraphs +id: paragraph.field_title +field_name: field_title +entity_type: paragraph +type: string +settings: + max_length: 255 + is_ascii: false + case_sensitive: false +module: core +locked: false +cardinality: 1 +translatable: true +indexes: { } +persist_with_no_fields: false +custom_storage: false diff --git a/config/default/default/node.type.should_have_been_page.yml b/config/default/default/node.type.should_have_been_page.yml new file mode 100644 index 0000000..8d2da9e --- /dev/null +++ b/config/default/default/node.type.should_have_been_page.yml @@ -0,0 +1,18 @@ +uuid: 421f0457-c81d-4608-8961-f68d40f7f725 +langcode: en +status: true +dependencies: + module: + - menu_ui +third_party_settings: + menu_ui: + available_menus: + - main-menu + parent: 'main-menu:0:' +name: 'Should Have Been Page' +type: should_have_been_page +description: '' +help: '' +new_revision: false +preview_mode: 1 +display_submitted: true diff --git a/config/default/default/node.type.test_paragraphs.yml b/config/default/default/node.type.test_paragraphs.yml new file mode 100644 index 0000000..cb6379f --- /dev/null +++ b/config/default/default/node.type.test_paragraphs.yml @@ -0,0 +1,18 @@ +uuid: 9a6654b2-1395-4a8b-8f01-dac8c0201665 +langcode: en +status: true +dependencies: + module: + - menu_ui +third_party_settings: + menu_ui: + available_menus: + - main-menu + parent: 'main-menu:0:' +name: 'Test Paragraphs' +type: test_paragraphs +description: '' +help: '' +new_revision: false +preview_mode: 1 +display_submitted: true diff --git a/config/default/default/paragraphs.paragraphs_type.title_body.yml b/config/default/default/paragraphs.paragraphs_type.title_body.yml new file mode 100644 index 0000000..a6418b3 --- /dev/null +++ b/config/default/default/paragraphs.paragraphs_type.title_body.yml @@ -0,0 +1,10 @@ +uuid: 5dc69ed5-cecb-43d5-8fb3-d02c517da478 +langcode: en +status: true +dependencies: { } +id: title_body +label: 'Title Body' +icon_uuid: null +icon_default: null +description: '' +behavior_plugins: { } diff --git a/docroot/modules/custom/sample_migration/sample_migration.module b/docroot/modules/custom/sample_migration/sample_migration.module index 6f46d94..c872554 100644 --- a/docroot/modules/custom/sample_migration/sample_migration.module +++ b/docroot/modules/custom/sample_migration/sample_migration.module @@ -6,6 +6,7 @@ */ use Drupal\Core\Routing\RouteMatchInterface; +use \Drupal\Core\Database\Database; /** * Implements hook_help(). @@ -28,8 +29,6 @@ function sample_migration_title_body_paragraphs(&$context) { /* @var NodeStorage $node_storage */ $node_storage = $entity_type_manager->getStorage('node'); - $media_storage = $entity_type_manager->getStorage('media'); - $file_storage = $entity_type_manager->getStorage('file'); if (empty($context['sandbox'])) { $context['sandbox'] = []; @@ -38,43 +37,51 @@ function sample_migration_title_body_paragraphs(&$context) { if (!isset($context['sandbox']['progress'])) { $context['sandbox']['progress'] = 0; $context['sandbox']['current_index'] = 0; - $all_arm_pages = $node_storage->loadByProperties([ - 'type' => 'arm_page' + $all_pages = $node_storage->loadByProperties([ + 'type' => 'page' ]); - $context['sandbox']['arm_content'] = array_values($all_arm_pages); - $context['sandbox']['arm_map'] = _get_arm_map(); - $context['sandbox']['file_map'] = _get_file_map(); - $context['sandbox']['d7_files'] = _get_d7_arm_files(); + $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']['arm_content']); - $context['sandbox']['max'] = count($context['sandbox']['arm_content']); + $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); foreach ($indexes as $index) { - $node = $context['sandbox']['arm_content'][$index]; - - if (!is_null($node)) { - $context['results'][] = $node->title->value; - $d7nid = $context['sandbox']['arm_map'][$node->id()]; - $d7file = $context['sandbox']['d7_files'][$d7nid->sourceid1]; - - if ($d7file) { - $d8file = $context['sandbox']['file_map'][$d7file->field_armpage_pdf_fid]; - $current_file = $file_storage->load($d8file->destid1); - - if ($d8file) { - $node->set('field_armpage_pdf', $current_file->id()); - $node->save(); + $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); + } } } } $context['sandbox']['progress']++; $context['sandbox']['current_index'] = $index; - $context['message'] = t('Now processing Reviews'); + $context['message'] = t('Now processing Paragraphs'); } // Update batch on our progress. @@ -86,7 +93,7 @@ function sample_migration_title_body_paragraphs(&$context) { } } -function sample_migration_title_body_paragraphs_finished() { +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)]); @@ -109,47 +116,98 @@ function sample_migration_title_body_paragraphs_finished() { function sample_migration_get_title_body_field_collections() { Database::setActiveConnection('drupal7'); $drupal7db = Database::getConnection(); - $query = $drupal7db->select('field_collection_item', 'fci') - ->fields('fci', [ - 'item_id', - 'field_name', - 'revision_id' - ]); - $query->innerJoin('field_data_' . $field_name, 'fd', 'fd.' . $field_name . '_value = fci.item_id'); - $query->fields('fd', [ + $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_name . '_revision_id' + 'field_fc_title_value' ]); - $query->condition('fci.field_name', $field_name); - - $query->leftJoin('field_data_field_usingpage_block_image', 'fdfubi', 'fdfubi.entity_id = fci.item_id'); - $query->fields('fdfubi', [ - 'entity_id', - 'revision_id', - 'field_usingpage_block_image_fid' + $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'); - if (count($results)) { - $drupal8db = Database::getConnection(); - $query = $drupal8db->select('migrate_map_upgrade_d7_node_using_library_page', 'mm'); - $query->fields('mm', [ - 'sourceid1', - 'destid1' - ]); - $map_results = $query->execute()->fetchAllAssoc('sourceid1'); - } + return $results; } function sample_migration_get_test_paragraph_content_map() { $drupal8db = Database::getConnection(); - $query = $drupal8db->select('migrate_map_upgrade_d7_node_using_library_page', 'mm'); + $query = $drupal8db->select('migrate_map_d7_node__test_paragraphs', 'mm'); $query->fields('mm', [ 'sourceid1', 'destid1' ]); $map_results = $query->execute()->fetchAllAssoc('sourceid1'); + return $map_results; +} + +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 + // 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)) { + // based on different parent node type the field we attach it to is differently + $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()); + } }