Skip to content
Snippets Groups Projects
Commit 0c43dce9 authored by Hervé Donner's avatar Hervé Donner
Browse files

ISAICP-9051: Refactor joinup_core_update_111200.

- Improve overall logic and add some comments
- Skip notifications when creating new document nodes
- Use accessCheck(FALSE) on entity queries
- Restore old redirects as-is
parent a564850f
No related branches found
No related tags found
1 merge request!184ISAICP-9051: Update hook from ISAICP-8911 should skip joinup notifications
......@@ -102,8 +102,9 @@ function joinup_core_requirements($phase): array {
*/
function joinup_core_update_111200(array &$sandbox): string {
if (!isset($sandbox['video_nids'])) {
// Collect all video nodes to migrate.
$video_nids = \Drupal::entityQuery('node')
->accessCheck()
->accessCheck(FALSE)
->condition('type', 'video')
->execute();
......@@ -112,13 +113,42 @@ function joinup_core_update_111200(array &$sandbox): string {
$sandbox['video_nids'] = $video_nids;
}
// Load video nodes.
$video_nids = array_slice($sandbox['video_nids'], $sandbox['count'], 20);
$entity_type_manager = \Drupal::entityTypeManager();
$video_nodes = $entity_type_manager->getStorage('node')->loadMultiple($video_nids);
/** @var \Drupal\redirect\RedirectRepository $redirect_repository */
$redirect_repository = \Drupal::service('redirect.repository');
// Migrate each video.
foreach ($video_nodes as $video_node) {
// Perform the conversion for each video node.
// Find redirects to the video node to preserve after deletion.
/* @see redirect_entity_delete */
$redirects = $redirect_repository->findByDestinationUri([
"internal:/node/{$video_node->id()}",
"entity:node/{$video_node->id()}",
]);
// Prepare new document node.
$formatted_date = (new DrupalDateTime())->setTimestamp($video_node->getCreatedTime())->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT);
$created_time = $video_node->getCreatedTime();
$document_node = Node::create([
'type' => 'document',
'nid' => $video_node->id(),
'title' => $video_node->getTitle(),
'uid' => $video_node->getOwnerId(),
'status' => $video_node->isPublished(),
'created' => $created_time,
'changed' => $video_node->getChangedTime(),
'og_audience' => $video_node->get('og_audience')->target_id,
'field_document_spatial_coverage' => $video_node->get('field_video_spatial_coverage')->target_id,
'field_keywords' => $video_node->get('field_keywords')->getValue(),
'field_document_publication_date' => $formatted_date,
'published_at' => $created_time,
'field_state' => 'published',
]);
$layout_paragraph = Paragraph::create(['type' => 'layout']);
$layout_paragraph->setBehaviorSettings('layout_paragraphs', [
'layout' => 'layout_onecol',
......@@ -148,69 +178,33 @@ function joinup_core_update_111200(array &$sandbox): string {
'region' => 'content',
]);
$query = \Drupal::entityQuery('redirect')
->accessCheck()
->condition('redirect_redirect.uri', 'internal:/node/' . $video_node->id());
$rids = $query->execute();
$redirects = $entity_type_manager->getStorage('redirect')->loadMultiple($rids);
$video_redirects = [];
foreach ($redirects as $redirect) {
$video_redirects[] = $redirect->getSourcePathWithQuery();
}
$video = [
'nid' => $video_node->id(),
'title' => $video_node->getTitle(),
'uid' => $video_node->getOwnerId(),
'status' => $video_node->isPublished(),
'created' => $video_node->getCreatedTime(),
'changed' => $video_node->getChangedTime(),
'og_audience' => $video_node->get('og_audience')->target_id,
'field_document_spatial_coverage' => $video_node->get('field_video_spatial_coverage')->target_id,
'field_keywords' => $video_node->get('field_keywords')->getValue(),
];
// Delete video.
$video_node->delete();
$formatted_date = (new DrupalDateTime())->setTimestamp($video['created'])->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT);
$document_node = Node::create([
'type' => 'document',
'nid' => $video['nid'],
'title' => $video['title'],
'uid' => $video['uid'],
'status' => $video['status'],
'created' => $video['created'],
'changed' => $video['changed'],
'og_audience' => $video['og_audience'],
'field_document_spatial_coverage' => $video['field_document_spatial_coverage'],
'field_keywords' => $video['field_keywords'],
'field_document_publication_date' => $formatted_date,
'published_at' => $video['created'],
'field_state' => 'published',
]);
$document_node->set('field_paragraphs_body', [
['entity' => $layout_paragraph],
['entity' => $body_paragraph],
['entity' => $video_paragraph],
]);
$document_node->setRevisionCreationTime($video['created']);
$document_node->save();
// Delete video node.
$video_node->delete();
foreach ($video_redirects as $redirect) {
$redirect = $entity_type_manager->getStorage('redirect')->create([
'redirect_source' => ['path' => $redirect],
'redirect_redirect' => ['uri' => 'internal:/node/' . $video['nid']],
'language' => 'und',
'status_code' => 301,
]);
$redirect->save();
}
// Save new document node.
// Create 2 revisions (1 published and 1 archived); un-archiving requires a
// previous published revision otherwise a fatal error occurs.
/* @see \Drupal\joinup_workflow\WorkflowHelper::unarchiveEntity */
$document_node->setRevisionCreationTime($created_time);
$document_node->skip_notification = TRUE;
$document_node->save();
$document_node->set('field_state', 'archived');
$document_node->setNewRevision();
$document_node->skip_notification = TRUE;
$document_node->save();
// Restore redirects.
foreach ($redirects as $redirect) {
$redirect->createDuplicate()->save();
}
$sandbox['count']++;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment