popsu_migrate: D7NodeProgramme extends D7Programme

This commit is contained in:
Bachir Soussi Chiadmi 2021-04-16 10:43:12 +02:00
parent 6edb82755b
commit eb4db23256
2 changed files with 220 additions and 215 deletions

View File

@ -15,6 +15,8 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drush\Drush;
use Drupal\popsu_migrate\Plugin\migrate\source\D7Programme;
/**
* Drupal 7 node source from database.
*
@ -23,92 +25,7 @@ use Drush\Drush;
* source_module = "node"
* )
*/
class D7NodeProgramme extends FieldableEntity {
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* The logger channel.
*
* @var \Drupal\Core\Logger\LoggerChannelInterface
*/
protected $logger;
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, StateInterface $state, EntityTypeManagerInterface $entity_manager, ModuleHandlerInterface $module_handler,LoggerChannelFactoryInterface $loggerFactory) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $state, $entity_manager);
$this->moduleHandler = $module_handler;
$this->logger = $loggerFactory->get('popsu_migration');
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$migration,
$container->get('state'),
$container->get('entity_type.manager'),
$container->get('module_handler'),
$container->get('logger.factory')
);
}
/**
* The join options between the node and the node_revisions table.
*/
const JOIN = 'n.vid = nr.vid';
/**
* {@inheritdoc}
*/
public function query() {
// Select node in its last revision.
$query = $this->select('node_revision', 'nr')
->fields('n', [
'nid',
'type',
'uid',
'language',
'status',
'created',
'changed',
])
->fields('nr', [
'vid',
'title',
'log',
'timestamp',
])
->orderBy('changed');
$query->addField('n', 'uid', 'node_uid');
$query->addField('nr', 'uid', 'revision_uid');
$query->innerJoin('node', 'n', static::JOIN);
if (isset($this->configuration['node_type'])) {
$query->condition('n.type', $this->configuration['node_type']);
}
// field_popsu_special_typetaxo 31 (Acueile)
$query->leftJoin('field_revision_field_popsu_special_typetaxo', 'ff', 'ff.revision_id = n.vid');
$query->fields('ff', [
'field_popsu_special_typetaxo_tid'
]);
$query->condition('ff.field_popsu_special_typetaxo_tid', 31);
return $query;
}
class D7NodeProgramme extends D7Programme {
/**
* {@inheritdoc}
@ -117,8 +34,6 @@ class D7NodeProgramme extends FieldableEntity {
$nid = $row->getSourceProperty('nid');
$vid = $row->getSourceProperty('vid');
$type = $row->getSourceProperty('type');
// $title = $row->getSourceProperty('title');
// drush_print('-- '.$nid."\t".$title);
// Get Field API field values.
foreach ($this->getFields('node', $type) as $field_name => $field) {
@ -130,7 +45,6 @@ class D7NodeProgramme extends FieldableEntity {
// title from taxo term
$field_popsu_special_popsu_tid = $row->getSourceProperty('field_popsu_special_popsu')[0]['tid'];
// Drush::output()->writeln('tid:' . $field_popsu_special_popsu_tid);
$q = $this->select('taxonomy_term_data', 'ttd')
->fields('ttd', ['tid','name'])
->condition('tid', $field_popsu_special_popsu_tid);
@ -143,31 +57,14 @@ class D7NodeProgramme extends FieldableEntity {
$row->setSourceProperty('body', $body[0]['value']);
$row->setSourceProperty('body_summary', $body[0]['summary']);
// texts
// texts (migration_lookup)
$row->setSourceProperty('text_paragraphe_id', $nid);
// $text = $row->getSourceProperty('field_popsu_special_text')
// $row->setSourceProperty('text', $text[0]['value']);
// $row->setSourceProperty('text_summary', $text[0]['summary']);
// // colloques
// $colloques = $this->getAssociatedSpecial(28, $field_popsu_special_popsu_tid);
// Drush::output()->writeln(dump($colloques));
//
// // Publications
// $publications = $this->getAssociatedSpecial(29, $field_popsu_special_popsu_tid);
// Drush::output()->writeln(dump($publications));
//
// // Partenaires
// $partenaires = $this->getAssociatedSpecial(30, $field_popsu_special_popsu_tid);
// Drush::output()->writeln(dump($partenaires));
// associateds
$associateds = $this->getAssociatedSpecial($field_popsu_special_popsu_tid);
Drush::output()->writeln(dump($associateds));
// fichiers
# field_popsu_special_insert_intro
# field_popsu_special_insert
$fichiers = array();
foreach ($row->getSourceProperty('field_popsu_special_insert') as $value) {
$fichiers[] = $value;
@ -178,19 +75,14 @@ class D7NodeProgramme extends FieldableEntity {
// files from associated nodes (colloques, publications, partenaires)
foreach ( ['colloques', 'publications'] as $name) {
foreach ($associateds[$name] as $key => $asso) {
// tried with array merge but it failed
// and apparently foreach is faster than array_merge
foreach ($asso['field_popsu_special_insert'] as $value) {
$fichiers[] = $value;
}
foreach ($asso['field_popsu_special_insert_intro'] as $value) {
$fichiers[] = $value;
}
// tried with array merge but it failed
// and foreach is faster than array_merge
// array_merge(
// $fichiers,
// $asso['field_popsu_special_insert_intro'],
// $asso['field_popsu_special_insert']
// );
}
}
$row->setSourceProperty('fichiers', $fichiers);
@ -198,105 +90,4 @@ class D7NodeProgramme extends FieldableEntity {
return parent::prepareRow($row);
}
/**
* {@inheritdoc}
*/
public function fields() {
$fields = [
'nid' => $this->t('Node ID'),
'type' => $this->t('Type'),
'title' => $this->t('Title'),
'node_uid' => $this->t('Node authored by (uid)'),
'revision_uid' => $this->t('Revision authored by (uid)'),
'created' => $this->t('Created timestamp'),
'changed' => $this->t('Modified timestamp'),
'status' => $this->t('Published'),
'promote' => $this->t('Promoted to front page'),
'sticky' => $this->t('Sticky at top of lists'),
'revision' => $this->t('Create new revision'),
'language' => $this->t('Language (fr, en, ...)'),
'tnid' => $this->t('The translation set id for this node'),
'timestamp' => $this->t('The timestamp the latest revision of this node was created.'),
];
return $fields;
}
/**
* {@inheritdoc}
*/
public function getIds() {
$ids['nid']['type'] = 'integer';
$ids['nid']['alias'] = 'n';
return $ids;
}
private function getAssociatedSpecial($filter_tid) {
$asso_tids = array(
"colloques" => 28,
"publications" => 29,
"partenaires" => 30
);
$associateds = array();
foreach ($asso_tids as $name => $type_tid) {
$query = $this->select('node_revision', 'nr')
->fields('n', [
'nid',
'type',
'uid',
'language',
'status',
'created',
'changed',
])
->fields('nr', [
'vid',
'title',
'log',
'timestamp',
])
->orderBy('changed');
$query->addField('n', 'uid', 'node_uid');
$query->addField('nr', 'uid', 'revision_uid');
$query->innerJoin('node', 'n', static::JOIN);
if (isset($this->configuration['node_type'])) {
$query->condition('n.type', $this->configuration['node_type']);
}
// field_popsu_special_typetaxo
// filter to get the right special type (colloques, publication, etc)
$query->leftJoin('field_revision_field_popsu_special_typetaxo', 'ff', 'ff.revision_id = n.vid');
$query->fields('ff', [
'field_popsu_special_typetaxo_tid'
]);
$query->condition('ff.field_popsu_special_typetaxo_tid', $type_tid);
// field_popsu_special_popsu
// filter to get right popsu (popsu 1, popsu europe, etc)
$query->leftJoin('field_revision_field_popsu_special_popsu', 'sp', 'sp.revision_id = n.vid');
$query->fields('sp', [
'field_popsu_special_popsu_tid'
]);
$query->condition('sp.field_popsu_special_popsu_tid', $filter_tid);
$assos = $query->execute()->fetchAll();
// get the fields
foreach ($assos as $key => $asso) {
// Get Field API field values.
foreach ($this->getFields('node', $asso['type']) as $field_name => $field) {
$assos[$key][$field_name] = $this->getFieldValues('node', $field_name, $asso['nid'], $asso['vid'], NULL);
}
}
if (count($assos)) {
// $associateds[$name] = array();
$associateds[$name] = $assos;
}
}
return $associateds;
}
}

View File

@ -0,0 +1,214 @@
<?php
// https://understanddrupal.com/articles/how-debug-drupal-migrations-part-2
namespace Drupal\popsu_migrate\Plugin\migrate\source;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\migrate\Row;
use Drupal\migrate_drupal\Plugin\migrate\source\d7\FieldableEntity;
use Drupal\Core\Database\Query\SelectInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandler;
use Drupal\Core\State\StateInterface;
use Drupal\migrate\Plugin\MigrationInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drush\Drush;
/**
* Drupal 7 node source from database.
*
* @MigrateSource(
* id = "d7_programme",
* source_module = "node"
* )
*/
class D7Programme extends FieldableEntity {
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* The logger channel.
*
* @var \Drupal\Core\Logger\LoggerChannelInterface
*/
protected $logger;
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, StateInterface $state, EntityTypeManagerInterface $entity_manager, ModuleHandlerInterface $module_handler,LoggerChannelFactoryInterface $loggerFactory) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $state, $entity_manager);
$this->moduleHandler = $module_handler;
$this->logger = $loggerFactory->get('popsu_migration');
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$migration,
$container->get('state'),
$container->get('entity_type.manager'),
$container->get('module_handler'),
$container->get('logger.factory')
);
}
/**
* The join options between the node and the node_revisions table.
*/
const JOIN = 'n.vid = nr.vid';
/**
* {@inheritdoc}
*/
public function query() {
// Select node in its last revision.
$query = $this->select('node_revision', 'nr')
->fields('n', [
'nid',
'type',
'uid',
'language',
'status',
'created',
'changed',
])
->fields('nr', [
'vid',
'title',
'log',
'timestamp',
])
->orderBy('changed');
$query->addField('n', 'uid', 'node_uid');
$query->addField('nr', 'uid', 'revision_uid');
$query->innerJoin('node', 'n', static::JOIN);
if (isset($this->configuration['node_type'])) {
$query->condition('n.type', $this->configuration['node_type']);
}
// field_popsu_special_typetaxo 31 (Acueile)
$query->leftJoin('field_revision_field_popsu_special_typetaxo', 'ff', 'ff.revision_id = n.vid');
$query->fields('ff', [
'field_popsu_special_typetaxo_tid'
]);
$query->condition('ff.field_popsu_special_typetaxo_tid', 31);
return $query;
}
/**
* {@inheritdoc}
*/
public function fields() {
$fields = [
'nid' => $this->t('Node ID'),
'type' => $this->t('Type'),
'title' => $this->t('Title'),
'node_uid' => $this->t('Node authored by (uid)'),
'revision_uid' => $this->t('Revision authored by (uid)'),
'created' => $this->t('Created timestamp'),
'changed' => $this->t('Modified timestamp'),
'status' => $this->t('Published'),
'promote' => $this->t('Promoted to front page'),
'sticky' => $this->t('Sticky at top of lists'),
'revision' => $this->t('Create new revision'),
'language' => $this->t('Language (fr, en, ...)'),
'tnid' => $this->t('The translation set id for this node'),
'timestamp' => $this->t('The timestamp the latest revision of this node was created.'),
];
return $fields;
}
/**
* {@inheritdoc}
*/
public function getIds() {
$ids['nid']['type'] = 'integer';
$ids['nid']['alias'] = 'n';
return $ids;
}
protected function getAssociatedSpecial($filter_tid) {
$asso_tids = array(
"colloques" => 28,
"publications" => 29,
"partenaires" => 30
);
$associateds = array();
foreach ($asso_tids as $name => $type_tid) {
$query = $this->select('node_revision', 'nr')
->fields('n', [
'nid',
'type',
'uid',
'language',
'status',
'created',
'changed',
])
->fields('nr', [
'vid',
'title',
'log',
'timestamp',
])
->orderBy('changed');
$query->addField('n', 'uid', 'node_uid');
$query->addField('nr', 'uid', 'revision_uid');
$query->innerJoin('node', 'n', static::JOIN);
if (isset($this->configuration['node_type'])) {
$query->condition('n.type', $this->configuration['node_type']);
}
// field_popsu_special_typetaxo
// filter to get the right special type (colloques, publication, etc)
$query->leftJoin('field_revision_field_popsu_special_typetaxo', 'ff', 'ff.revision_id = n.vid');
$query->fields('ff', [
'field_popsu_special_typetaxo_tid'
]);
$query->condition('ff.field_popsu_special_typetaxo_tid', $type_tid);
// field_popsu_special_popsu
// filter to get right popsu (popsu 1, popsu europe, etc)
$query->leftJoin('field_revision_field_popsu_special_popsu', 'sp', 'sp.revision_id = n.vid');
$query->fields('sp', [
'field_popsu_special_popsu_tid'
]);
$query->condition('sp.field_popsu_special_popsu_tid', $filter_tid);
$assos = $query->execute()->fetchAll();
// get the fields
foreach ($assos as $key => $asso) {
// Get Field API field values.
foreach ($this->getFields('node', $asso['type']) as $field_name => $field) {
$assos[$key][$field_name] = $this->getFieldValues('node', $field_name, $asso['nid'], $asso['vid'], NULL);
}
}
if (count($assos)) {
// $associateds[$name] = array();
$associateds[$name] = $assos;
}
}
return $associateds;
}
}