3

My goal is to import my own data set into Drupal 8 using Migrate modules (migrate_plus, migrate_tools). In drupal, I use Paragraph module, and here my trouble starts. I've build my own plugin to transform my own data into paragraph. The transformed data is good if i create a new node and attached to it into the transform() function but that's not what I want, it's only to say that my data may be correct (see comments below)

I do import with base field like text, tags or file, and with this, the node will be generated, but I can't figure out why 'return $paragraphs' won't anything.

PS : I un-install/install my module (and the conf migrate_plus generated) everytime I change my .yml file and cache rebuild.

  • Drupal (8.1.1)
    • migrate_plus (8.x-2.x-dev)
    • migrate_tools (8.x-2.0-beta1)
    • migrate_source_json (8.x-2.x-dev)
    • paragraphs (8.x-1.0-rc4)

Migration configuration for articles (config/install/migrate_plus.migration.article.yml)

id: article
label: Article
migration_group: Articles
migration_dependencies: {}

source:
  plugin: json_source
  path: http://www.example.com/dataset.json
  headers:
    Accept: 'application/json'
  identifier: id
  identifierDepth: 0
  fields:
    - id
    - title
    - paragraphs

destination:
  plugin: entity:node

process:
  type:
    plugin: default_value
    default_value: article

  title: title

  field_paragraphes: 
      - source: paragraphs
        plugin: paragraphs_import

  uid:
    plugin: default_value
    default_value: 0

Dataset exemple

[{
    "id": "1",
    "title": "article 1",
    "paragraphs": [{
        "id": "1",
        "title": "paragraph 1 article 1"
    }, {
        "id": "2",
        "title": "paragraph 2 article 1"
    }]
}, {
    "id": "2",
    "title": "article 2",
    "paragraphs": [{
        "id": "3",
        "title": "paragraph 1 article 2"
    }, {
        "id": "4",
        "title": "paragraph 2 article 2"
    }]
}]

Custom plugin used on 'field_paragraphes' (/src/Plugin/migrate/process/ParagraphsImport.php)

<?php

/**
 * @file
 * Contains \Drupal\my_module\Plugin\migrate\process\ParagraphsImport.
 */

namespace Drupal\my_module\Plugin\migrate\process;

use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row;
use Drupal\node\Entity\Node;
use Drupal\paragraphs\Entity\Paragraph;

/**
 *
 * @MigrateProcessPlugin(
 *   id = "paragraphs_import"
 * )
 */
class ParagraphsImport extends ProcessPluginBase {
  /**
   * {@inheritdoc}
   */
  public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
    // code only to add 1 paragraph to my node, to make thing easier to start

    // paragraph_txtimg = one paragraph type allowed into my paragraph field

    $paragraphs=array();
    $ppt_values = array(
      'id' => NULL,
      'type' => 'paragraph_txtimg',
    );
    $ppt_paragraph = Paragraph::create($ppt_values);
    $ppt_paragraph->save();



    // IF I uncomment this part, it will create a new node with a paragrah, this works
    // But this is not what I want to do, I want to return the paragraph to the node that will be created by migrate
    /*
      $node = Node::create(array(
        'type' => 'article',
        'title' => 'This is a new article with a fresh paragraph generated !',
        'field_paragraphes' => array(
          array(
            'target_id' => $ppt_paragraph->id(),
            'target_revision_id' => $ppt_paragraph->getRevisionId(),
          ),
        ),
      ));
      $node->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;
  }

}
1

I finaly found the missing key :
transform() is called on EACH item of the source.

field_paragraphes: 
 - source: paragraphs
   plugin: paragraphs_import # will be called for each value of source: paragraphs

And so, the return value should be the paragraph revision definition :

<?php

/**
 * @file
 * Contains \Drupal\my_module\Plugin\migrate\process\ParagraphsImport.
 */

namespace Drupal\my_module\Plugin\migrate\process;

use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row;
use Drupal\node\Entity\Node;
use Drupal\paragraphs\Entity\Paragraph;

/**
 *
 * @MigrateProcessPlugin(
 *   id = "paragraphs_import"
 * )
 */
class ParagraphsImport extends ProcessPluginBase {
  /**
   * {@inheritdoc}
   */
  public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
    // transform() is called on each source value

    $paragraph_options = array(
      'id' => NULL,
      'type' => 'paragraph_txtimg',
    );
    $paragraph = Paragraph::create($paragraph_options);
    $paragraph->save();

    return $paragraph;
  }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.