|
| 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); |
0 commit comments