|
|
@ -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()); |
|
|
|
} |
|
|
|
} |