Score:0

Migrating one field into multiple rows

kn flag

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;
  }

}
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.