I am trying to migrate html code into multiple paragraphs that need to be linked together somehow. My idea is to have them linked my ID that comes from the dataset, maybe using that ID as second source. My code currently creates multiple paragraphs but there is no link between them. The data comes form csv an I am using Drupal 9
TLDR: How to map one column from csv file into multiple rows in mapping table.
The yml code ( data is the html code)
process:
type:
plugin: default_value
default_value: link
langcode: kieli
title: Nimi
default_langcode:
plugin: default_value
default_value: true
field_multi_paragraphs:
- plugin: paragraph_generate
entity_type: paragraph
source: Data #useless
value_key: id
values:
field_data: Data
destination:
plugin: 'entity_reference_revisions:paragraph'
The code that generates the paragraphs
/**
*
* @MigrateProcessPlugin(
* id = "paragraph_generate"
* )
*/
class ParagraphGenerate extends EntityGenerate {
public function transform($value, MigrateExecutableInterface $migrateExecutable, Row $row, $destinationProperty) {
$this->row = $row;
$this->migrateExecutable = $migrateExecutable;
$result = $this->generateEntity($value);
return $result;
}
protected function generateEntity($value) {
$dom = new DOMDocument();
$dom->loadHTML('<?xml encoding="utf-8" ?>' . $value);
$html = $dom->getElementsByTagName('body')->item(0);
$returnArray= [];
foreach ($html->childNodes as $child) {
if ($child->tagName === 'p') {
$paragraph = Paragraph::create([
'type' => 'text',
'field_text' => array(
"value" => $child->nodeValue,
"format" => "plain"
),
]);
$paragraph->save();
$returnArray[] = ['target_id' => $paragraph->id(), 'target_revision_id' => $paragraph->getRevisionId()];
}
}
return empty($returnArray) ? NULL : $returnArray;
}
}