lerxstrulz
lerxstrulz

Reputation: 153

Drupal 8 Issue Importing Paragraphs

I need to import some custom field data from a Drupal 6 instance into a Drupal 8 instance using Paragraphs. Since D8 stores all translations in a single node instead of separate linked nodes like D6, I am having an problem trying to get translated content into a Paragraph and inserted into a node (page.) The default language (English) works fine. Here is my code (I am importing from a JSON file that was a dump from the D6 instance):

... Code to read JSON file here and load into $data variable ...

// create paragraph ($fields is an array of fields from the JSON file)
$paragraph = Paragraph::create($fields);

// load existing node
$node = Node::load($nodeId);

// Get the translated node...according to the docs, this should 
// return a node that behaves just like the original node
$language = \Drupal::languageManager()->getLanguage($data['language'])->getId();
$node = $node->getTranslation($language);

$paragraphs = $node->field_paragraph_group;
$paragraphs[] = $paragraph;

$node->field_paragraph_group = $paragraphs;
$node->save();

It appears to save ok but once this is run the website stops working with

The website encountered an unexpected error. Please try again later.

so it's obviously breaking something.

I'm not sure if I need to try and access the translation from the node or add the translation in the Paragraph object, something like:

$paragraph->language = $data['language'];

Any guidance would be appreciated! Thank you!

Upvotes: 0

Views: 1261

Answers (1)

Aless_io
Aless_io

Reputation: 96

Answering to your comment :

According to the lead dev the migrations won't work as it is for flexfields data converting to Paragraphs.

With migration you are completely free to manage your input and output data, no matter what format they are stored in your old site.

You have many options to expose your D6 data but the easier and more flexible way is to use a migrate source plugin as Drupal 8 comes with some helpful plugins to extract and manage data from older Drupal version like 6 and 7. Have a look for example to the class: Drupal\node\Plugin\migrate\source\d6\Node

Regarding your issue I would suggest to do as following:

1) Create a source plugin that runs an SQL query to gather your D6 data (https://www.drupal.org/docs/8/api/migrate-api/migrate-source )

2) Create a process plugin to manipulate the incoming data to reflect the D8 paragraph structure (https://www.drupal.org/docs/8/api/migrate-api/migrate-process-plugins)

3) Use the core destination plugin entity:node You can leverage on the class that contains useful methods to extract D6 nodes.

You will probably need a few custom queries and row preprocess so here is a simple example of a source plugin (yourmodule/src/Plugin/migrate/source/MyCustomMigration.php):

<?php

/**
 * @file
 * Contains \Drupal\custom_d6_migration\Plugin\migrate\source\MyCustomMigration.
 */

namespace Drupal\custom_d6_migration\Plugin\migrate\source;

use Drupal\migrate\Row;
use Drupal\node\Plugin\migrate\source\d6\Node;

/**
 * Source plugin for D6 content.
 *
 * @MigrateSource(
 *   id = "my_custom_migration"
 * )
 */
class MyCustomMigration extends Node {

  /**
   * {@inheritdoc}
   */
  public function query() {
    // .. your custom query $this->select('tablename'); ...
  }

  /**
   * {@inheritdoc}
   */
  public function prepareRow(Row $row) {
    // If you need to alter the current row (running further queries to gather additional data..)..

    return $row;
  }

}

Even if this question was posted months ago I hope this answer can still be helpful for someone else as migration is really helpful to solve almost any kind of data migration issue.

Upvotes: 0

Related Questions