Date: Thu, 28 Mar 2024 09:30:30 +0000 (UTC) Message-ID: <1785129458.375.1711618230227@adff906d7572> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_374_1703774108.1711618230227" ------=_Part_374_1703774108.1711618230227 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html
UN= DER REVIEW 230530
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 c= ase of migrating a Date field.
You can alter a data by extending the ProcessPluginBase and using t= he transform method.
You should of run the drush version of&nb= sp;custom migration and created a module. We will need to edit on= e of the config YML files.
In this case we are changing the node import which is usual identified l= ike: migrate_plus.migration.upgrade_d7_node_YOURTYPE.yml
We are going to change our date field in the yml from:
field_CU= STOM_DATE: field_CUSTOM_DATE
We are changing the process handler to:
field_CU= STOM_DATE: plugin: fix_date source: field_CUSTOM_DATE
In your custom migration module create the following folder path: src/Pl= ugin/migrate/process
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 =3D "fix_date", * ) */ class FixDate extends ProcessPluginBase { /** * {@inheritdoc} */ public function transform($value, MigrateExecutableInterface $migrate_exe= cutable, Row $row, $destination_property) { try { if (is_numeric($value)) { $date =3D new \DateTime(); $date->setTimestamp($value); } elseif (is_array($value)) { $value =3D implode(',', $value); $date =3D new \DateTime($value); } else { $date =3D new \DateTime($value); } $value =3D $date->format('Y-m-d\TH:i:s'); } catch (\Exception $e) { throw new MigrateException('Invalid source date.'); } return $value; } }
Who |
When |
Status |
---|---|---|
Bob |
20230530 |
Requested someone to review in Slack. I don=E2=80=99t have recent expert= ise. |