0

I want to migrate the Drupal 7 body of my blog to a Drupal 8 paragraph (field_stories_content which is Entity reference revisions).
The body is to be assign to the field_stories_text_formatted field.

1- I have my first migration (mm_d7_node_blog_body_to_paragraph) with a source of d7_node, and a destination of entity_reference_revisions:paragraph to create the paragraphs using the plugin mmNodeBlogBodyToParagraph.php.
2- I do the actual node migration where I lookup the result of the first migration and use it in the process for my new paragraph field (field_stories_text_formatted) in the node migration (mm_d7_node_blog).

node__field_stories_content (TABLE, Entity reference revisions field)   
bundle     
entity_id     
revision_id 
field_stories_content_target_id    
field_stories_content_target_revision_id   

paragraph__field_stories_text_formatted  (TABLE, paragraph)    
bundle   
entity_id     
revision_id   
field_stories_text_formatted_value    

entity_id is field_stories_content_target_id    
revision_id is field_stories_content_target_revision_id

But It is not processing and I don't know why. What am I missing?

This is what I have so far.

migrate_plus.migration.mm_d7_mm_blog.yml

langcode: en
status: true
dependencies: {  }
id: mm_d7_node_blog
label: 'Blog -> Article (Nodes)'
source:
  plugin: d7_node
  node_type: blog
destination:
  plugin: 'entity:node'
migration_dependencies: {  }
process:
  type:
    plugin: default_value
    default_value: article

field_stories_content/target_id:    
  plugin: migration_lookup
  migration: mm_d7_node_blog_body_to_paragraph    
  no_stub: true
  source: nid

field_stories_content/target_revision_id:
  plugin: migration_lookup
  migration: mm_d7_node_blog_body_to_paragraph
  no_stub: true
  source: nid

migrate_plus.migration.mm_d7_node_blog_body_to_paragraph.yml

  langcode: en
  status: true
  dependencies: {  }
  id: mm_d7_node_blog_body_to_paragraph
  source:
    plugin: d7_node
    node_type: blog
  destination:
    plugin: 'entity_reference_revisions:paragraph'
    default_bundle: field_stories_text_formatted   
  migration_dependencies:
    required:
      - mm_node_blog_body_to_paragraph
  process:
    field_stories_text_formatted:    
      plugin: mm_node_blog_body_to_paragraph   
      no_stub: true
      'field_stories_text_formatted/value': body
      'field_stories_text_formatted/format':
         plugin: default_value
         default_value: full_html
       source: nid

mmNodeBlogBodyToParagraph.php

<?php

namespace Drupal\mm_migrate\Plugin\migrate\process;

use Drupal\migrate\MigrateException;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\Row;

use Drupal\node\Entity\Node;
use Drupal\paragraphs\Entity\Paragraph;

/**
* Provides a 'MmNodeBlogBodyToParagraph' migrate process plugin.
*
* @MigrateProcessPlugin(
*  id = "mm_node_blog_body_to_paragraph",
* )
*/
class MmNodeBlogBodyToParagraph extends ProcessPluginBase  {
  /**
 * {@inheritdoc}
 */
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {

    $body = $row->getSourceProperty("field_body")[0]["value"];
    $nid = $row->getSourceProperty("nid");
    $paragraph_text_values = array(
   'field_stories_text_formatted' => array(
     'bundle' => 'stories_text',
     'value' => $body,
     'format' => 'full_html',
   ),
  );
      $paragraph_text_paragraph = Paragraph::create($paragraph_text_values);
      var_dump("--------------------------------PARAGRAPH paragraph_text *********** ------------------------------------");
      var_dump($paragraph_text_values);
      $paragraph_text_paragraph->save();

      $target_id_dest = $paragraph_text_paragraph->Id();
      $target_revision_id_dest = $paragraph_text_paragraph->getRevisionId();
      //node__field_stories_content (TABLE) -> field_stories_content_target_id, field_stories_content_target_revision_id
      //paragraph__field_stories_text_formatted  (TABLE) -> bundle, entity_id, revision_id, field_stories_text_formatted_value
      //entity_id => field_stories_content_target_id & revision_id => field_stories_content_target_revision_id

     $paragraphs[] = array('target_id' => $target_id_dest, 'target_revision_id' => $target_revision_id_dest);

      $node = entity_load('node', $nid);
      $node->field_stories_content = array( $paragraph_text_paragraph);
      //$node->save();
      return paragraphs;
   }
}    

----------- Update based on @Hudri feedback ----------

migrate_plus.migration.mm_d7_mm_blog.yml

langcode: en
status: true
dependencies: {  }
id: mm_d7_node_blog
label: 'Blog -> Article (Nodes)'
source:
  plugin: d7_node
  node_type: blog
destination:
  plugin: 'entity:node'
migration_dependencies:
  required:
    - mm_d7_node_blog_body_to_paragraph
  optional: {  }
process:
  type:
    plugin: default_value
    default_value: article

  title: title

  field_stories_content:
-
  plugin: migration_lookup
  migration: mm_d7_node_blog_body_to_paragraph
  no_stub: true
-
  plugin: iterator
  process:
    target_id: '0'
    target_revision_id: '1'  

migrate_plus.migration.mm_d7_node_blog_body_to_paragraph.yml

langcode: en
status: true
dependencies: {  }
id: mm_d7_node_blog_body_to_paragraph
label: 'Blog body -> Paragraph'
source:
plugin: d7_node
node_type: blog
destination:
  plugin: 'entity_reference_revisions:paragraph'
  default_bundle: field_stories_text_formatted       
migration_dependencies:
  required: { }
  optional: { }
process:
  field_stories_text_formatted/value:    
    plugin: entity_generate
    source: field_stories_text_formatted
    default_values: 
    'field_stories_text_formatted/value': 'field_body/0/value'
    'field_stories_text_formatted/format':
       plugin: default_value
       default_value: 'full_html'
     no_stub: true
     bundle: 'stories_text'   

After making changes it is still not processing. Am I missing something

  • Seeing only what you have shared here, I doubt this will work. Show the annotation of class MmNodeBlogBodyToParagraph. What about paragraphs_to_migrate migration? What about mm_node_blog_body_to_paragraph? – Stefan Korn Oct 15 '18 at 8:17
  • @StefanKorn, thanks for the feedback. I updated the question, but it is still not processing. Do I need another plugin? – mamesaye Oct 15 '18 at 15:08
  • What is mmNodeBlogBodyToParagraph.php expected to do (in natural words)? A process plugin should transform values, a $node->save() in there seems REALLY weird. – Hudri Oct 15 '18 at 15:30
  • mmNodeBlogBodyToParagraph.php is suppose to get the body of a blog node and create a paragraph entity revision field and return the newly created paragraph so that it can be used in the next migration. – mamesaye Oct 15 '18 at 16:10
  • 1
    This is not how the Migration module works. The source plugin should fetch/provide the data. A process plugin should only transform the data provided by the source plugin, It must not create entities used by the migration. The destination plugin describes the target entity to be created. Migrations need this structure to map source and destination entities. You could create entities manually yourself by code... but when you do this, it doesn't make sense to use migration module at all. – Hudri Oct 15 '18 at 19:11
0

This is what work for me (hope it helps):
migrate_plus.migration.mm_d7_node_blog.yml

langcode: en
status: true
dependencies: {  }
id: mm_d7_node_blog
label: 'Blog -> Article (node)'
source:
  plugin: d7_node
  node_type: blog
destination:
  plugin: 'entity:node'
process:
  #create temporary field to call plugin that create the paragraph
  _field_stories_text_formatted:
    plugin: mm_node_blog_body_to_paragraph_plugin
    source: 'field_body/0/format'      
title: title
field_stories_content/target_id:     
no_stub: true
plugin: extract
source: '@_field_stories_text_formatted'
index:
  - 0
  - entity_id
field_stories_content/target_revision_id:
no_stub: true
plugin: extract
source: '@_field_stories_text_formatted'
index:
  - 0
  - revision_id  
migration_dependencies:
  required:
    - mm_node_blog_body_to_paragraph_plugin

mmNodeBlogBodyToParagraph.php

<?php

namespace Drupal\mm_migrate\Plugin\migrate\process;

use Drupal\migrate\MigrateException;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\Row;

use Drupal\node\Entity\Node;
use Drupal\paragraphs\Entity\Paragraph;

/**
  * Provides a 'MmNodeBlogBodyToParagraph' migrate process plugin.
  *
  * @MigrateProcessPlugin(
  *  id = "mm_node_blog_body_to_paragraph_plugin",
  * )
  * *
  * To do create a paragraph type field_stories_text_formatted:
  *
  * @code
  * field_stories_text_formatted:
  *   plugin: mm_node_blog_body_to_paragraph_plugin
  *   source: 'field_body/0/format'
  * @endcode
  *
  */


class MmNodeBlogBodyToParagraph extends ProcessPluginBase  {
  /**
 * {@inheritdoc}
 */
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {   
   $body = $row->getSourceProperty("field_body")[0]["value"];   

   $ppt_values = array(
       'type' => 'stories_text',
        'bundle' => 'stories_text',
        'field_stories_text_formatted' => array(
          'value' => $body,
          'format' => 'full_html',
        ),
      );   

  $ppt_paragraph = Paragraph::create($ppt_values);
      $ppt_paragraph->save();

      $target_id_dest = $ppt_paragraph->id();
      $target_revision_id_dest = $ppt_paragraph->getRevisionId();
      $paragraphs[] = [ 'entity_id' => $target_id_dest, 'revision_id' => $target_revision_id_dest, ];     

        return $paragraphs;
    }
}

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.