Skip to content

Commit

Permalink
LOYALIST-44 Begin migrate Blog nodes, title/body fields
Browse files Browse the repository at this point in the history
  • Loading branch information
jtmcd75 committed Sep 4, 2024
1 parent 90dca1f commit b1bb1f1
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
id: 1_loyalist_migrate_loyalist_blog
label: 'Loyalist Blog Posts'

migration_tags:
- Loyalist
- SQL

source:
plugin: loyalist_blog
key: migrate

process:
nid: nid
vid: vid
status: status
created: created
changed: changed
promote: promote
sticky: sticky
title:
plugin: callback
callable: trim
source: title
'body/format':
plugin: default_value
default_value: wysiwyg
'body/value': body_value
'body/summary': body_summary

destination:
plugin: 'entity:node'
default_bundle: blog_post
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace Drupal\loyalist_migrate\Plugin\migrate\source;

use Drupal\migrate\Annotation\MigrateSource;
use Drupal\migrate\Plugin\migrate\source\SqlBase;
use Drupal\migrate\Row;

/**
* Migrates Loyalist Blog Posts.
*
* @MigrateSource(
* id = "loyalist_blog",
* source_module = "loyalist_migrate",
* )
*/
class BlogPost extends SqlBase
{
/**
* {@inheritdoc}
*/
public function query()
{
$query = $this->select('node', 'n');
$query->condition('n.type', 'blog');
$query->addField('n', 'nid', 'nid');
$query->addField('n', 'vid', 'vid');
$query->addField('n', 'title', 'title');
$query->addField('n', 'status', 'status');
$query->addField('n', 'created', 'created');
$query->addField('n', 'changed', 'changed');
$query->addField('n', 'promote', 'promote');
$query->addField('n', 'sticky', 'sticky');
$query->leftJoin('field_data_body', 'fb', 'n.nid = fb.entity_id AND fb.deleted = 0');
$query->addField('fb', 'body_value', 'body_value');
$query->addField('fb', 'body_summary', 'body_summary');

return $query;
}

/**
* {@inheritdoc}
*/
public function fields()
{
// This maps the field from their name above to a destination field name that is specified in the process section. I generally keep them the same.
$fields = [
'nid' => 'nid',
'vid' => 'vid',
'title' => 'title',
'status' => 'status',
'created' => 'created',
'changed' => 'changed',
'promote' => 'promote',
'sticky' => 'sticky',
'body_value' => 'body_value',
'body_summary' => 'body_summary',
];

return $fields;
}

/**
* {@inheritdoc}
*/
public function getIds()
{
return [
'nid' => [
'type' => 'integer',
'alias' => 'n',
],
];
}

/**
* {@inheritdoc}
*/
public function prepareRow(Row $row)
{
/*
Example of how to manipulate data using prepareRow.
We should generally use process plugins in the yaml definition instead.
$date = $row->getSourceProperty('date');
$time = $row->getSourceProperty('time');
$datetime = $date . 'T' . $time . ':00';
$row->setSourceProperty('datetime', $datetime);
*/

return parent::prepareRow($row);
}
}

0 comments on commit b1bb1f1

Please sign in to comment.