more migration
This commit is contained in:
@@ -43,14 +43,18 @@ 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'
|
||||
-
|
||||
name: artistes
|
||||
label: 'Artistes invité·es'
|
||||
selector: 'Artistes invité·es'
|
||||
-
|
||||
name: partners
|
||||
label: 'Partenaires associés'
|
||||
selector: 'Partenaires associés'
|
||||
# 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.
|
||||
@@ -91,9 +95,25 @@ process:
|
||||
-
|
||||
plugin: skip_on_empty
|
||||
method: process
|
||||
source: invites
|
||||
source: curator
|
||||
-
|
||||
plugin: personne_generate
|
||||
type_de_personne: curateur
|
||||
field_artiste_s_invite_s:
|
||||
-
|
||||
plugin: skip_on_empty
|
||||
method: process
|
||||
source: artistes
|
||||
-
|
||||
plugin: personne_generate
|
||||
type_de_personne: artiste
|
||||
field_partenaire_s:
|
||||
-
|
||||
plugin: skip_on_empty
|
||||
method: process
|
||||
source: partners
|
||||
-
|
||||
plugin: partenaire_generate
|
||||
uid:
|
||||
plugin: default_value
|
||||
default_value: 3
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user