Created
October 19, 2016 08:57
-
-
Save pfaocle/4b945309f18716a5f24c759fa0d9ae8b to your computer and use it in GitHub Desktop.
Migrate D6 node body into D8 Paragraphs field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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