From dfefc56268c513bfdd0a31825f1cb3c0c9f72f3b Mon Sep 17 00:00:00 2001 From: bach Date: Sat, 11 Jul 2026 16:39:46 +0200 Subject: [PATCH] fixed date field migration on projet_node --- .../migrate_plus.migration.projet_node.yml | 10 +-- .../src/Plugin/migrate/process/LeshedDate.php | 77 +++++++++++++++++++ 2 files changed, 81 insertions(+), 6 deletions(-) create mode 100644 web/modules/custom/migrate_leshed/src/Plugin/migrate/process/LeshedDate.php diff --git a/web/modules/custom/migrate_leshed/config/install/migrate_plus.migration.projet_node.yml b/web/modules/custom/migrate_leshed/config/install/migrate_plus.migration.projet_node.yml index 0de5500..9dd4477 100644 --- a/web/modules/custom/migrate_leshed/config/install/migrate_plus.migration.projet_node.yml +++ b/web/modules/custom/migrate_leshed/config/install/migrate_plus.migration.projet_node.yml @@ -63,15 +63,13 @@ process: default_value: projet title: title field_dates/value: - plugin: format_date + plugin: leshed_date source: date_start - from_format: 'd/m/Y' - to_format: 'Y-m-d' field_dates/end_value: - plugin: format_date + plugin: leshed_date source: date_end - from_format: 'd/m/Y' - to_format: 'Y-m-d' + end_of_year: true + fallback_source: date_start field_type_de_projet: - plugin: skip_on_empty diff --git a/web/modules/custom/migrate_leshed/src/Plugin/migrate/process/LeshedDate.php b/web/modules/custom/migrate_leshed/src/Plugin/migrate/process/LeshedDate.php new file mode 100644 index 0000000..3f364c5 --- /dev/null +++ b/web/modules/custom/migrate_leshed/src/Plugin/migrate/process/LeshedDate.php @@ -0,0 +1,77 @@ +convert($value, $migrate_executable, $destination_property); + + if ($result === NULL && !empty($this->configuration['fallback_source'])) { + $fallback = $row->getSourceProperty($this->configuration['fallback_source']); + $result = $this->convert($fallback, $migrate_executable, $destination_property); + } + + return $result; + } + + /** + * Converts a single raw value to 'Y-m-d', or NULL if not parseable. + */ + private function convert(mixed $value, MigrateExecutableInterface $migrate_executable, string $destination_property): ?string { + if (!is_string($value)) { + return NULL; + } + $value = trim($value); + if ($value === '') { + return NULL; + } + + // Bare year: "2016". + if (preg_match('/^\d{4}$/', $value)) { + return empty($this->configuration['end_of_year']) + ? $value . '-01-01' + : $value . '-12-31'; + } + + // Full date: "19/09/2015". + $date = \DateTime::createFromFormat('!d/m/Y', $value); + $errors = \DateTime::getLastErrors(); + if ($date === FALSE || ($errors && ($errors['warning_count'] || $errors['error_count']))) { + $migrate_executable->saveMessage( + sprintf('Date invalide "%s" pour la propriété "%s".', $value, $destination_property) + ); + return NULL; + } + + return $date->format('Y-m-d'); + } + +}