Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.55 KB | None | 0 0
  1. #Миграция новости с d7 на d8
  2. id: migrate_explode_node_news
  3. label: Migrate explode node news
  4. migration_group: migrate_explode
  5. dependencies:
  6. enforced:
  7. module:
  8. - migrate_explode
  9. source:
  10. plugin: migrate_explode_node_news
  11. #provide the drupal7 connection name here
  12. target: migrate
  13. #specify the node types that to be migrarted
  14. node_type: news
  15. file: field_image_teaser
  16. constants:
  17. file_destination: 'public://news/img_teaser/'
  18. process:
  19. nid: nid
  20. type:
  21. plugin: default_value
  22. default_value: news
  23. langcode:
  24. plugin: default_value
  25. source: language
  26. default_value: "und"
  27. title: title
  28. status: status
  29. created: created
  30. changed: changed
  31. promote: promote
  32. sticky: sticky
  33. path: alias
  34.  
  35. field_news:
  36. plugin: array_build
  37. key: tid
  38. value: tid
  39. source: field_news
  40.  
  41.  
  42. field_image_teaser:
  43. plugin: image_import
  44. source: url
  45. target_id: field_img_teaser_fid
  46. destination: constants/file_destination
  47. title: title
  48. alt: field_img_teaser_alt
  49. width: field_img_teaser_width
  50. height: field_img_teaser_height
  51. skip_on_missing_source: true
  52.  
  53. field_paragraphs: paragraphs
  54.  
  55. destination:
  56. #how to store the data fetched from the source
  57. plugin: entity:node
  58. migration_dependencies:
  59. required:
  60. - migrate_explode_term
  61. ____________________________________________________
  62. Это source plugin
  63. Пока пробую мигрировать только одну конкретную новость.
  64. <?php
  65.  
  66. namespace Drupal\migrate_explode\Plugin\migrate\source;
  67.  
  68. use Drupal\migrate\Row;
  69. use Drupal\node\Plugin\migrate\source\d7\Node;
  70. use Drupal\paragraphs\Entity\Paragraph;
  71.  
  72. /**
  73. * @MigrateSource(
  74. * id = "migrate_explode_node_news"
  75. * )
  76. */
  77. class NodeNews extends Node {
  78.  
  79. public function query() {
  80. $query = parent::query();
  81. $query->condition('n.nid', 121);
  82. return $query;
  83. }
  84.  
  85. /**
  86. * {@inheritdoc}
  87. */
  88. public function prepareRow(Row $row) {
  89. $nid = $row->getSourceProperty('nid');
  90.  
  91. // Prepare paragraphs array.
  92. $paragraphs = [];
  93.  
  94. // News Terms Referrence Field.
  95. $news_terms = [];
  96. $result = $this->select('field_data_field_news', 'fdn')
  97. ->fields('fdn', ['field_news_tid'])
  98. ->condition('fdn.entity_id', $nid)
  99. ->execute();
  100. while ($record = $result->fetchObject()) {
  101. $news_terms[] = $record->field_news_tid;
  102. }
  103. if (!empty($news_terms)) {
  104. $row->setSourceProperty('news_terms', $news_terms);
  105. }
  106.  
  107. // Migrate URL alias.
  108. $query = $this->select('url_alias', 'ua')
  109. ->fields('ua', ['alias', 'pid'])
  110. ->condition('ua.source', 'node/' . $nid);
  111. $alias = $query->execute()->fetchAllAssoc('pid');
  112. if (!empty($alias)) {
  113. $alias = '/' . end($alias)['alias'];
  114. $row->setSourceProperty('alias', $alias);
  115. }
  116.  
  117. // Body field with value.
  118. $result = $this->select('field_data_body', 'fdb')
  119. ->fields('fdb', ['body_value'])
  120. ->condition('fdb.entity_id', $nid)
  121. ->execute();
  122. while ($record = $result->fetchObject()) {
  123. $body = $record->body_value;
  124. }
  125. if (!empty($body)) {
  126. // Create paragraph;
  127. $paragraph = Paragraph::create([
  128. 'type' => 'text',
  129. 'field_body' => [
  130. 'value' => strip_tags($body, '<p><a><br><i><b><strong><em>'),
  131. 'format' => "full_html"
  132. ],
  133. ]);
  134. $paragraph->save();
  135. $paragraphs[] = $paragraph->id();
  136. }
  137.  
  138. // Migrate teaser image
  139. $path_to_file = 'http://d7drupalSite.loc/sites/default/files/news/img_teaser/';
  140. $query = $this->select('field_data_field_img_teaser', 'fdfit')
  141. ->fields('fdfit', [
  142. 'field_img_teaser_fid',
  143. 'field_img_teaser_alt',
  144. 'field_img_teaser_width',
  145. 'field_img_teaser_height'])
  146. ->condition('fdfit.entity_id', $nid);
  147. $image = $query->execute()->fetchAllAssoc('field_img_teaser_fid');
  148. if (!empty($image)) {
  149. $query = $this->select('file_managed', 'fm')
  150. ->fields('fm', ['filename'])
  151. ->condition('fm.fid', $image['field_img_teaser_fid']);
  152. $image_name = $query->execute()->fetchAllAssoc('fid');
  153. if (!empty($imge_name)) {
  154. $url = $path_to_file . $image_name;
  155. $row->setSourceProperty('url', $url);
  156. $row->setSourceProperty('field_image_teaser', $image);
  157. }
  158. }
  159.  
  160. // Set paragraphs field.
  161. if (!empty($paragraphs)) {
  162. $row->setSourceProperty('paragraphs', $paragraphs);
  163. }
  164.  
  165. return parent::prepareRow($row);
  166. }
  167.  
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement