Occasionally you may need to change up the data as it is migrating from your D6 / D7 site to your D8 site. This is common especially in the case of migrating a Date field.
You can alter a data by extending the ProcessPluginBase and using the transform method.
We are going to change our date field in the yml from:
field_CUSTOM_DATE: field_CUSTOM_DATE |
We are changing the process handler to:
field_CUSTOM_DATE: plugin: fix_date source: field_CUSTOM_DATE |
Create a file in the new folder called FixDate.php and put this code in:
<?php namespace Drupal\YOUR-CUSTOM-MODULE\Plugin\migrate\process; use Drupal\migrate\MigrateException; use Drupal\migrate\MigrateExecutableInterface; use Drupal\migrate\ProcessPluginBase; use Drupal\migrate\Row; /** * Convert a date field to the proper format. * * @MigrateProcessPlugin( * id = "fix_date", * ) */ class FixDate extends ProcessPluginBase { /** * {@inheritdoc} */ public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) { try { if (is_numeric($value)) { $date = new \DateTime(); $date->setTimestamp($value); } elseif (is_array($value)) { $value = implode(',', $value); $date = new \DateTime($value); } else { $date = new \DateTime($value); } $value = $date->format('Y-m-d\TH:i:s'); } catch (\Exception $e) { throw new MigrateException('Invalid source date.'); } return $value; } } |