Drupal 9: Change content type of certain nodes during migration

29 Aug.2022
Image
Migration

Question: I have two content types that have the same fields. For certain nodes, I would like to convert from one node type to another. How can I accomplish this?

Reply: Where custom_node_type would make the necessary determination based on the node source data whether to return 'page' or 'policy'.

File
modules/custom/MYMODULE/src/Plugin/migrate/process/CustomNodeType.php
<?php
 
namespace Drupal\MYMODULE\Plugin\migrate\process;
 
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row;
 
/**
 * Dynamically update content type depends on...
 *
 * @MigrateProcessPlugin(
 *   id = "custom_node_type"
 * )
 */
class CustomNodeType extends ProcessPluginBase {
 
  /**
   * {@inheritdoc}
   */
  public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
    $nid = $row->getSourceProperty('nid');
 
 
    $SOMETHING = ''; // Write your logic.
    if ($SOMETHING) {
      return 'policy';
    }
 
    return $row->getSourceProperty('node_type');
  }
 
}
 
?>

On *.yml file, you need to add this plugin under the process section.

File
modules/custom/MYMODULE/config/install/migrate_plus.migration.node_page.yml
destination:
  plugin: 'entity:node'
  default_bundle: page
source:
  plugin: kwall_node_original_alias
  node_type: page
process:
  type:
    plugin: custom_node_type
  nid:
    - plugin: get
      source: tnid
  vid:
    - plugin: get
      source: vid