more migration

This commit is contained in:
2026-07-12 18:55:08 +02:00
parent dfefc56268
commit 47a4f04c65
17 changed files with 699 additions and 165 deletions
@@ -0,0 +1,100 @@
<?php
declare(strict_types=1);
namespace Drupal\migrate_leshed\Plugin\migrate\process;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\migrate\Attribute\MigrateProcess;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Finds or creates 'partenaire' nodes from a comma-separated list of names.
*
* Input: a string such as "Port Autonome de Rouen, Labelle 69". Names are
* split on commas and trimmed. Empty values and placeholders ("/") are
* skipped. An existing node with the same title is reused, otherwise it is
* created. Returns an array of node IDs.
*/
#[MigrateProcess(id: 'partenaire_generate', handle_multiples: TRUE)]
final class PartenaireGenerate extends ProcessPluginBase implements ContainerFactoryPluginInterface {
/**
* Placeholder values that must not create a partenaire node.
*/
private const IGNORED_VALUES = ['/', '?', '-'];
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
private readonly EntityTypeManagerInterface $entityTypeManager,
) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): self {
return new self(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('entity_type.manager'),
);
}
/**
* {@inheritdoc}
*/
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property): array {
if (!is_string($value) || trim($value) === '') {
return [];
}
$names = array_filter(array_map('trim', explode(',', $value)));
$nids = [];
foreach ($names as $name) {
if (in_array($name, self::IGNORED_VALUES, TRUE)) {
continue;
}
$nids[] = $this->getOrCreatePartenaire($name);
}
return $nids;
}
/**
* Returns the nid of the matching partenaire node, creating it if needed.
*/
private function getOrCreatePartenaire(string $title): int {
$storage = $this->entityTypeManager->getStorage('node');
$ids = $storage->getQuery()
->condition('type', 'partenaire')
->condition('title', $title)
->accessCheck(FALSE)
->range(0, 1)
->execute();
if ($ids) {
return (int) reset($ids);
}
$node = $storage->create([
'type' => 'partenaire',
'title' => $title,
'status' => 1,
'uid' => $this->configuration['author_uid'] ?? 3,
]);
$node->save();
return (int) $node->id();
}
}
@@ -20,6 +20,12 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
* commas. For each name, the first word is used as 'field_prenom' and the
* rest as the node title. An existing node with the same title and prenom
* is reused, otherwise it is created. Returns an array of node IDs.
*
* When 'type_de_personne' is set in the configuration, a taxonomy term with
* that name is found or created (without duplicates) in the
* 'type_de_personne' vocabulary, and referenced from the node's
* 'field_type_de_personne' field. The tag is also added to matching
* existing nodes that do not yet have it.
*/
#[MigrateProcess(id: 'personne_generate', handle_multiples: TRUE)]
final class PersonneGenerate extends ProcessPluginBase implements ContainerFactoryPluginInterface {
@@ -92,6 +98,9 @@ final class PersonneGenerate extends ProcessPluginBase implements ContainerFacto
private function getOrCreatePersonne(string $nom, string $prenom): int {
$storage = $this->entityTypeManager->getStorage('node');
$type_name = $this->configuration['type_de_personne'] ?? NULL;
$tid = $type_name ? $this->getOrCreateType($type_name) : NULL;
$query = $storage->getQuery()
->condition('type', 'personne')
->condition('title', $nom)
@@ -105,19 +114,67 @@ final class PersonneGenerate extends ProcessPluginBase implements ContainerFacto
}
$ids = $query->execute();
if ($ids) {
return (int) reset($ids);
$nid = (int) reset($ids);
if ($tid !== NULL) {
$this->addTypeToPersonne($nid, $tid);
}
return $nid;
}
$node = $storage->create([
$values = [
'type' => 'personne',
'title' => $nom,
'field_prenom' => $prenom === '' ? NULL : $prenom,
'status' => 1,
'uid' => $this->configuration['author_uid'] ?? 3,
]);
];
if ($tid !== NULL) {
$values['field_type_de_personne'] = $tid;
}
$node = $storage->create($values);
$node->save();
return (int) $node->id();
}
/**
* Adds a type_de_personne term to an existing node if not already set.
*/
private function addTypeToPersonne(int $nid, int $tid): void {
$node = $this->entityTypeManager->getStorage('node')->load($nid);
if ($node === NULL) {
return;
}
$existing = array_column($node->get('field_type_de_personne')->getValue(), 'target_id');
if (!in_array((string) $tid, array_map('strval', $existing), TRUE)) {
$node->get('field_type_de_personne')->appendItem(['target_id' => $tid]);
$node->save();
}
}
/**
* Returns the tid of the matching type_de_personne term, creating it if needed.
*/
private function getOrCreateType(string $name): int {
$storage = $this->entityTypeManager->getStorage('taxonomy_term');
$ids = $storage->getQuery()
->condition('vid', 'type_de_personne')
->condition('name', $name)
->accessCheck(FALSE)
->range(0, 1)
->execute();
if ($ids) {
return (int) reset($ids);
}
$term = $storage->create([
'vid' => 'type_de_personne',
'name' => $name,
]);
$term->save();
return (int) $term->id();
}
}