Email us at info@henrytech.org to discuss your Drupal Migration today!
Sometimes during a migration, we need a process plugin, which alters the value before it gets stored in the new database. This example is useful if the original values have too many characters to fit into the new field.
Create a new file as your_custom_module/src/Plugin/migrate/process/SkipOnTooLong.php:
<?php /** * @file * Contains \Drupal\your_custom_module\Plugin\migrate\process\SkipOnTooLong. * */ namespace Drupal\atdove_migrate\Plugin\migrate\process; use Drupal\migrate\MigrateSkipProcessException; use Drupal\migrate\ProcessPluginBase; use Drupal\migrate\MigrateExecutableInterface; use Drupal\migrate\Row; use Drupal\migrate\MigrateSkipRowException; /** * Skips processing the current row when the input value is too long. * * @see \Drupal\migrate\Plugin\MigrateProcessInterface * * @MigrateProcessPlugin( * id = "skip_on_too_long" * ) */ class SkipOnTooLong extends ProcessPluginBase { /** * Skips the current row when value is not set. * * @param mixed $value * The input value. * @param \Drupal\migrate\MigrateExecutableInterface $migrate_executable * The migration in which this process is being executed. * @param \Drupal\migrate\Row $row * The row from the source to process. * @param string $destination_property * The destination property currently worked on. This is only used together * with the $row above. * * @return mixed * The input value, $value, if it is not empty. * * @throws \Drupal\migrate\MigrateSkipRowException * Thrown if the source property is not set and the row should be skipped, * records with STATUS_IGNORED status in the map. */ public function row($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) { if (strlen($value) > 50) { $message = !empty($this->configuration['message']) ? $this->configuration['message'] : ''; throw new MigrateSkipRowException($message); } return $value; } /** * Stops processing the current property when value is not set. * * @param mixed $value * The input value. * @param \Drupal\migrate\MigrateExecutableInterface $migrate_executable * The migration in which this process is being executed. * @param \Drupal\migrate\Row $row * The row from the source to process. * @param string $destination_property * The destination property currently worked on. This is only used together * with the $row above. * * @return mixed * The input value, $value, if it is not empty. * * @throws \Drupal\migrate\MigrateSkipProcessException * Thrown if the source property is not set and rest of the process should * be skipped. */ public function process($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) { if (!$value) { throw new MigrateSkipProcessException(); } return $value; } }
Comments