Skip to content

Commit 50463f2

Browse files
committed
BKDK-399 section pages migration fix
1 parent ee4065f commit 50463f2

File tree

4 files changed

+191
-9
lines changed

4 files changed

+191
-9
lines changed
Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1 @@
11
<?php
2-
3-
function ballerup_d7_migration_create_url_from_nid($nid) {
4-
return 'test/' . $nid;
5-
}
6-
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
<?php
2+
3+
/**
4+
* This script migrates images and list of related node to nodes.
5+
*
6+
* See task: https://os2web.atlassian.net/browse/BKDK-399
7+
*/
8+
9+
use Drupal\ballerup_d7_migration\Utility\MigrationHelper;
10+
use Drupal\Core\Database\Database;
11+
use Drupal\file\FileInterface;
12+
use Drupal\node\Entity\Node;
13+
use Drupal\paragraphs\Entity\Paragraph;
14+
15+
$database = \Drupal::database();
16+
$migrateDatabase = Database::getConnection('default', 'migrate');
17+
$migrationHelper = new MigrationHelper();
18+
19+
$relatedLinksToProcess = [];
20+
21+
// Counters.
22+
$countPicturesUpdated = 0;
23+
$countRelatedPagesParagraphsAdded = 0;
24+
25+
$query = $migrateDatabase->select('taxonomy_term_data', 'term')
26+
->condition('vid', "2");
27+
$query->leftJoin('field_data_field_section_image', 'image', 'image.entity_id = term.tid');
28+
$query->leftJoin('field_data_field_perferred_links_multiple', 'links', 'links.entity_id = term.tid');
29+
$query->fields('term', []);
30+
$query->fields('image', ['field_section_image_fid']);
31+
$query->fields('links', ['field_perferred_links_multiple_target_id']);
32+
$terms = $query->execute()->fetchAll();
33+
34+
foreach ($terms as $term) {
35+
$termTitle = $term->name;
36+
37+
$hasPicture = !empty($term->field_section_image_fid);
38+
$hasLink = !empty($term->field_perferred_links_multiple_target_id);
39+
40+
// Adding pictured to the nodes.
41+
if ($hasPicture) {
42+
// Find page with the name.
43+
$query = \Drupal::entityQuery('node')
44+
->condition('type', 'os2web_page')
45+
->condition('title', $termTitle);
46+
$results = $query->execute();
47+
48+
if (empty($results)) {
49+
continue;
50+
}
51+
52+
$nodes = Node::loadMultiple($results);
53+
54+
foreach ($nodes as $node) {
55+
// Check if has picture, if not set it.
56+
if (!$node->field_os2web_page_primaryimage->target_id) {
57+
$sourceFileUrl = $migrationHelper->getFileDownloadUrl(['fid' => $term->field_section_image_fid]);
58+
$file_name = basename($sourceFileUrl);
59+
60+
// Saving file.
61+
$directory = 'public://';
62+
$file = system_retrieve_file($sourceFileUrl, $directory . '/' . $file_name, TRUE);
63+
64+
if ($file instanceof FileInterface) {
65+
$node->field_os2web_page_primaryimage->target_id = $file->id();
66+
$node->save();
67+
$countPicturesUpdated++;
68+
}
69+
}
70+
}
71+
}
72+
73+
if ($hasLink) {
74+
// Find page with the name.
75+
$query = \Drupal::entityQuery('node')
76+
->condition('type', 'os2web_page')
77+
->condition('title', $termTitle);
78+
$results = $query->execute();
79+
80+
if (empty($results)) {
81+
continue;
82+
}
83+
84+
$nodes = Node::loadMultiple($results);
85+
86+
// Create an array of nodes that need links to be added.
87+
foreach ($nodes as $node) {
88+
$sourceNodeId = $term->field_perferred_links_multiple_target_id;
89+
90+
// Find local page representing this target id.
91+
$localNodeId = $migrationHelper->findLocalNode($sourceNodeId);
92+
if ($localNodeId) {
93+
$relatedLinksToProcess[$node->id()][]['target_id'] = $localNodeId;
94+
}
95+
}
96+
}
97+
}
98+
99+
100+
// Processing related links.
101+
foreach ($relatedLinksToProcess as $nid => $links) {
102+
$node = Node::load($nid);
103+
104+
// Does this node already have content reference paragraph.
105+
$right_paragraphs = $node->get('field_os2web_page_paragraph_righ')->referencedEntities();
106+
if (!empty($right_paragraphs)) {
107+
$break = FALSE;
108+
109+
/** @var \Drupal\paragraphs\Entity\Paragraph $par */
110+
foreach ($right_paragraphs as $par) {
111+
if ($par->getType() == 'os2web_content_reference') {
112+
$break = TRUE;
113+
}
114+
}
115+
116+
// Breaking the loop if this node already have content reference paragraph.
117+
if ($break) {
118+
continue;
119+
}
120+
}
121+
122+
// Creating content reference paragraph.
123+
if (!empty($links)) {
124+
$paragraph = Paragraph::create([
125+
'type' => 'os2web_content_reference',
126+
'field_os2web_content_reference_h' => 'MEST BESØGTE SIDER',
127+
'field_os2web_content_ref_vmod' => 'list',
128+
'field_os2web_content_reference' => $links,
129+
]);
130+
$paragraph->save();
131+
132+
// Getting ol paragraphs.
133+
$right_paragraphs_field = $node->field_os2web_page_paragraph_righ;
134+
135+
// Adding a new one.
136+
$right_paragraphs_field[] = [
137+
'target_id' => $paragraph->id(),
138+
'target_revision_id' => $paragraph->getRevisionId(),
139+
];
140+
141+
// Eventually saving.
142+
$node->field_os2web_page_paragraph_righ = $right_paragraphs_field;
143+
$node->save();
144+
$countRelatedPagesParagraphsAdded++;
145+
}
146+
}
147+
148+
print_r('Images updated: ' . $countPicturesUpdated);
149+
print_r(PHP_EOL);
150+
print_r('New related pages blocks added: ' . $countRelatedPagesParagraphsAdded);
151+
print_r(PHP_EOL);

web/modules/custom/ballerup_d7_migration/scripts/migrate.sh

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,15 @@ echo "Migration ballerup_d7_node_indholdside - START"
4848
drush migrate:import ballerup_d7_node_indholdside --update
4949
echo "Migration ballerup_d7_node_indholdside - END"
5050

51-
echo "Execuing custom script [1/4] - Fix publish status"
51+
echo "Execuing custom script [1/5] - Fix publish status"
5252
drush scr modules/custom/ballerup_d7_migration/scripts/migrate_fix_publish_status.php
53-
echo "Execuing custom script [2/4] - Remove inline picutres"
53+
echo "Execuing custom script [2/5] - Remove inline picutres"
5454
drush scr modules/custom/ballerup_d7_migration/scripts/remove_inline_pictures.php
55-
echo "Execuing custom script [3/4] - Remove node header duplicates (content pages using Borger.dk article)"
55+
echo "Execuing custom script [3/5] - Remove node header duplicates (content pages using Borger.dk article)"
5656
drush scr modules/custom/ballerup_d7_migration/scripts/BKDK-401-migrate_remove_page_borgerdk_body.php
57-
echo "Execuing custom script [4/4] - Disale related links block on migrated nodes"
57+
echo "Execuing custom script [4/5] - Disable related links block on migrated nodes"
5858
drush scr modules/custom/ballerup_d7_migration/scripts/BKDK-432-migrate_disable_related_links.php
59+
echo "Execuing custom script [5/5] - Updated section pages"
60+
drush scr modules/custom/ballerup_d7_migration/scripts/BKDK-399-update_section_pages.php
5961

6062
echo "Migration complete visit URL '/admin/config/system/delete-orphans' to delete the orphaned paragraphs"

web/modules/custom/ballerup_d7_migration/src/Utility/MigrationHelper.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,4 +295,38 @@ function createDigitalPostLink($field_link) {
295295
return $digitalPostLink;
296296
}
297297

298+
/**
299+
* Helper function to find local node by remote ID.
300+
*
301+
* @param $sourceNodeId
302+
* Remote node ID.
303+
*
304+
* @return int|null
305+
* Int if the local node is found. NULL otherwise.
306+
*/
307+
function findLocalNode($sourceNodeId) {
308+
$node_migrate_tables = [
309+
'migrate_map_ballerup_d7_node_gallery_slide',
310+
'migrate_map_ballerup_d7_node_indholdside',
311+
'migrate_map_ballerup_d7_node_institution_page',
312+
'migrate_map_ballerup_d7_node_news',
313+
];
314+
315+
$database = \Drupal::database();
316+
foreach ($node_migrate_tables as $table) {
317+
$localNid = $database->select($table)->fields($table, [
318+
'destid1',
319+
])
320+
->condition('sourceid1', $sourceNodeId)
321+
->execute()
322+
->fetchField();
323+
324+
if ($localNid) {
325+
return $localNid;
326+
}
327+
}
328+
329+
return NULL;
330+
}
331+
298332
}

0 commit comments

Comments
 (0)