From a2a0e423a31dd2439edbb27ec1083ccfbfd77dea Mon Sep 17 00:00:00 2001 From: bach Date: Sat, 11 Jul 2026 16:06:12 +0200 Subject: [PATCH] =?UTF-8?q?added=20'invit=C3=A9/accompagn=C3=A9/curat?= =?UTF-8?q?=C3=A9=20par'=20field=20to=20migration,=20with=20CT=20personne?= =?UTF-8?q?=20node=20creation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- googlesheet/Projets.json | 2 +- .../migrate_plus.migration.projet_node.yml | 32 ++++- .../migrate/process/PersonneGenerate.php | 123 ++++++++++++++++++ .../src/Plugin/migrate/source/ProjetNode.php | 4 +- 4 files changed, 158 insertions(+), 3 deletions(-) create mode 100644 web/modules/custom/migrate_leshed/src/Plugin/migrate/process/PersonneGenerate.php diff --git a/googlesheet/Projets.json b/googlesheet/Projets.json index 2e9fa05..f9274cf 100644 --- a/googlesheet/Projets.json +++ b/googlesheet/Projets.json @@ -799,7 +799,7 @@ "Le Shed - La maison, Maromme", "2022", "", - "Accompagnement pro", + "Accompagnement professionnel", "avec", "", "RN13BIS, RRouen" 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 6f0b032..0de5500 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 @@ -43,6 +43,14 @@ source: name: projet_type label: 'Type de projet' selector: 'Type de projet' + - + name: invites + label: 'Invité/accompagné/curaté par' + selector: 'invité/accompagné/curaté par' + - + name: curator + label: 'Commissaria' + selector: 'invité/accompagné/curaté par' # Under 'ids', we identify source fields populated above which will uniquely # identify each imported item. The 'type' makes sure the migration map table # uses the proper schema type for stored the IDs. @@ -65,7 +73,29 @@ process: from_format: 'd/m/Y' to_format: 'Y-m-d' field_type_de_projet: - + - + plugin: skip_on_empty + method: process + source: projet_type + - + plugin: explode + delimiter: ',' + - + plugin: callback + callable: trim + - + plugin: entity_generate + entity_type: taxonomy_term + bundle_key: vid + bundle: type_de_projet + value_key: name + field_invite_accompagne_curate_p: + - + plugin: skip_on_empty + method: process + source: invites + - + plugin: personne_generate uid: plugin: default_value default_value: 3 diff --git a/web/modules/custom/migrate_leshed/src/Plugin/migrate/process/PersonneGenerate.php b/web/modules/custom/migrate_leshed/src/Plugin/migrate/process/PersonneGenerate.php new file mode 100644 index 0000000..0fc2daf --- /dev/null +++ b/web/modules/custom/migrate_leshed/src/Plugin/migrate/process/PersonneGenerate.php @@ -0,0 +1,123 @@ +get('entity_type.manager'), + ); + } + + /** + * {@inheritdoc} + */ + public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property): array { + if (!is_string($value) || trim($value) === '') { + return []; + } + + // Ignore everything after "avec" (e.g. "Camille Bondon avec les élèves + // de l'école André Marie" only keeps "Camille Bondon"). + $value = preg_replace('/\s+avec\s+.*$/us', '', $value); + + // Normalize separators: " et " becomes a comma. + $value = preg_replace('/\s+et\s+/u', ',', $value); + $names = array_filter(array_map('trim', explode(',', $value))); + + $nids = []; + foreach ($names as $name) { + if (in_array($name, self::IGNORED_VALUES, TRUE)) { + continue; + } + // First word is the first name, the rest is the last name. + $parts = preg_split('/\s+/u', $name, 2); + $prenom = $parts[0]; + $nom = $parts[1] ?? ''; + // Single-word name: use it as the title, without a first name. + if ($nom === '') { + $nom = $prenom; + $prenom = ''; + } + $nids[] = $this->getOrCreatePersonne($nom, $prenom); + } + + return $nids; + } + + /** + * Returns the nid of the matching personne node, creating it if needed. + */ + private function getOrCreatePersonne(string $nom, string $prenom): int { + $storage = $this->entityTypeManager->getStorage('node'); + + $query = $storage->getQuery() + ->condition('type', 'personne') + ->condition('title', $nom) + ->accessCheck(FALSE) + ->range(0, 1); + if ($prenom !== '') { + $query->condition('field_prenom', $prenom); + } + else { + $query->notExists('field_prenom'); + } + $ids = $query->execute(); + if ($ids) { + return (int) reset($ids); + } + + $node = $storage->create([ + 'type' => 'personne', + 'title' => $nom, + 'field_prenom' => $prenom === '' ? NULL : $prenom, + 'status' => 1, + 'uid' => $this->configuration['author_uid'] ?? 3, + ]); + $node->save(); + + return (int) $node->id(); + } + +} diff --git a/web/modules/custom/migrate_leshed/src/Plugin/migrate/source/ProjetNode.php b/web/modules/custom/migrate_leshed/src/Plugin/migrate/source/ProjetNode.php index adc2b6f..8b2c400 100644 --- a/web/modules/custom/migrate_leshed/src/Plugin/migrate/source/ProjetNode.php +++ b/web/modules/custom/migrate_leshed/src/Plugin/migrate/source/ProjetNode.php @@ -25,7 +25,9 @@ final class ProjetNode extends Url { 'id' => $this->t('Project ID'), 'title' => $this->t('Project Title'), 'date_start' => $this->t('Project date start'), - 'date_end' => $this->t('Project date end') + 'date_end' => $this->t('Project date end'), + 'projet_type' => $this->t('Type de projet'), + 'curator' => $this->t('Commissaria') ]; }