Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Миграция новости с d7 на d8
- id: migrate_explode_node_news
- label: Migrate explode node news
- migration_group: migrate_explode
- dependencies:
- enforced:
- module:
- - migrate_explode
- source:
- plugin: migrate_explode_node_news
- #provide the drupal7 connection name here
- target: migrate
- #specify the node types that to be migrarted
- node_type: news
- file: field_image_teaser
- constants:
- file_destination: 'public://news/img_teaser/'
- process:
- nid: nid
- type:
- plugin: default_value
- default_value: news
- langcode:
- plugin: default_value
- source: language
- default_value: "und"
- title: title
- status: status
- created: created
- changed: changed
- promote: promote
- sticky: sticky
- path: alias
- field_news:
- plugin: array_build
- key: tid
- value: tid
- source: field_news
- field_image_teaser:
- plugin: image_import
- source: url
- target_id: field_img_teaser_fid
- destination: constants/file_destination
- title: title
- alt: field_img_teaser_alt
- width: field_img_teaser_width
- height: field_img_teaser_height
- skip_on_missing_source: true
- field_paragraphs: paragraphs
- destination:
- #how to store the data fetched from the source
- plugin: entity:node
- migration_dependencies:
- required:
- - migrate_explode_term
- ____________________________________________________
- Это source plugin
- Пока пробую мигрировать только одну конкретную новость.
- <?php
- namespace Drupal\migrate_explode\Plugin\migrate\source;
- use Drupal\migrate\Row;
- use Drupal\node\Plugin\migrate\source\d7\Node;
- use Drupal\paragraphs\Entity\Paragraph;
- /**
- * @MigrateSource(
- * id = "migrate_explode_node_news"
- * )
- */
- class NodeNews extends Node {
- public function query() {
- $query = parent::query();
- $query->condition('n.nid', 121);
- return $query;
- }
- /**
- * {@inheritdoc}
- */
- public function prepareRow(Row $row) {
- $nid = $row->getSourceProperty('nid');
- // Prepare paragraphs array.
- $paragraphs = [];
- // News Terms Referrence Field.
- $news_terms = [];
- $result = $this->select('field_data_field_news', 'fdn')
- ->fields('fdn', ['field_news_tid'])
- ->condition('fdn.entity_id', $nid)
- ->execute();
- while ($record = $result->fetchObject()) {
- $news_terms[] = $record->field_news_tid;
- }
- if (!empty($news_terms)) {
- $row->setSourceProperty('news_terms', $news_terms);
- }
- // Migrate URL alias.
- $query = $this->select('url_alias', 'ua')
- ->fields('ua', ['alias', 'pid'])
- ->condition('ua.source', 'node/' . $nid);
- $alias = $query->execute()->fetchAllAssoc('pid');
- if (!empty($alias)) {
- $alias = '/' . end($alias)['alias'];
- $row->setSourceProperty('alias', $alias);
- }
- // Body field with value.
- $result = $this->select('field_data_body', 'fdb')
- ->fields('fdb', ['body_value'])
- ->condition('fdb.entity_id', $nid)
- ->execute();
- while ($record = $result->fetchObject()) {
- $body = $record->body_value;
- }
- if (!empty($body)) {
- // Create paragraph;
- $paragraph = Paragraph::create([
- 'type' => 'text',
- 'field_body' => [
- 'value' => strip_tags($body, '<p><a><br><i><b><strong><em>'),
- 'format' => "full_html"
- ],
- ]);
- $paragraph->save();
- $paragraphs[] = $paragraph->id();
- }
- // Migrate teaser image
- $path_to_file = 'http://d7drupalSite.loc/sites/default/files/news/img_teaser/';
- $query = $this->select('field_data_field_img_teaser', 'fdfit')
- ->fields('fdfit', [
- 'field_img_teaser_fid',
- 'field_img_teaser_alt',
- 'field_img_teaser_width',
- 'field_img_teaser_height'])
- ->condition('fdfit.entity_id', $nid);
- $image = $query->execute()->fetchAllAssoc('field_img_teaser_fid');
- if (!empty($image)) {
- $query = $this->select('file_managed', 'fm')
- ->fields('fm', ['filename'])
- ->condition('fm.fid', $image['field_img_teaser_fid']);
- $image_name = $query->execute()->fetchAllAssoc('fid');
- if (!empty($imge_name)) {
- $url = $path_to_file . $image_name;
- $row->setSourceProperty('url', $url);
- $row->setSourceProperty('field_image_teaser', $image);
- }
- }
- // Set paragraphs field.
- if (!empty($paragraphs)) {
- $row->setSourceProperty('paragraphs', $paragraphs);
- }
- return parent::prepareRow($row);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement