Skip to content

Instantly share code, notes, and snippets.

@pfaocle
Created October 19, 2016 08:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pfaocle/4b945309f18716a5f24c759fa0d9ae8b to your computer and use it in GitHub Desktop.
Save pfaocle/4b945309f18716a5f24c759fa0d9ae8b to your computer and use it in GitHub Desktop.
Migrate D6 node body into D8 Paragraphs field
<?php
/**
* @file
* Migrate D6 node body into D8 Paragraphs field.
*
* @see http://www.amitgoyal.in/2016/03/d6-d8-migration-d6-body-d8-paragraph-type-text.html
*/
namespace Drupal\ixis_migrate_from_d6\Plugin\migrate\process;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\Row;
use Drupal\node\Entity\Node;
use Drupal\paragraphs\Entity\Paragraph;
/**
* Saves D6 Page Body field to D8 Page Paragraph (Textarea) field.
*
* @MigrateProcessPlugin(
* id = "node_paragraph_textarea"
* )
*/
class NodeParagraphTextarea extends ProcessPluginBase {
/**
* {@inheritdoc}
*/
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
$is_new = 1;
$paragraphs = [];
$body = $value;
$nid = $row->getSource()['nid'];
$node = Node::load($nid);
$field_type_paragraphs = [];
if (!empty($node->field_paragraphs)) {
$field_type_paragraphs = $node->field_paragraphs->getValue();
}
// Loop through all the paragraph types associated with the node.
foreach ($field_type_paragraphs as $paragraph_source) {
$target_id = $paragraph_source['target_id'];
$target_revision_id = $paragraph_source['target_revision_id'];
$paragraph_data = Paragraph::load($target_id);
if ($paragraph_data->bundle() == 'text') {
$paragraph_text = array(
'value' => $body,
'format' => 'ckeditor',
);
$paragraph_data->set('field_text', $paragraph_text);
$paragraph_data->save();
$is_new = 0;
}
// All the existing paragraphs types will be captured.
// This is done to avoid removal of existing paragraphs types.
$paragraphs[] = ['target_id' => $target_id, 'target_revision_id' => $target_revision_id];
}
// If no text is assigned to node, create a new paragraph.
if ($is_new) {
$ppt_values = array(
'id' => NULL,
'type' => 'text',
'field_text' => array(
'value' => $body,
'format' => 'ckeditor',
),
);
$ppt_paragraph = Paragraph::create($ppt_values);
$ppt_paragraph->save();
$target_id_dest = $ppt_paragraph->Id();
$target_revision_id_dest = $ppt_paragraph->getRevisionId();
$paragraphs[] = array('target_id' => $target_id_dest, 'target_revision_id' => $target_revision_id_dest);
}
return $paragraphs;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment