From 2a4c49c8dc4fc8ce465738a9e00f8c1dd3cff959 Mon Sep 17 00:00:00 2001 From: Bachir Soussi Chiadmi Date: Thu, 30 Nov 2017 08:12:11 +0100 Subject: [PATCH] autres sons migration done --- .../migrate_plus.migration.d6_edlp_sons.yml | 101 +++++++++ .../src/Plugin/migrate/source/D6EdlpSons.php | 193 ++++++++++++++++++ 2 files changed, 294 insertions(+) create mode 100644 sites/all/modules/figli/edlp_migrate/config/install/migrate_plus.migration.d6_edlp_sons.yml create mode 100644 sites/all/modules/figli/edlp_migrate/src/Plugin/migrate/source/D6EdlpSons.php diff --git a/sites/all/modules/figli/edlp_migrate/config/install/migrate_plus.migration.d6_edlp_sons.yml b/sites/all/modules/figli/edlp_migrate/config/install/migrate_plus.migration.d6_edlp_sons.yml new file mode 100644 index 000000000..74eae7449 --- /dev/null +++ b/sites/all/modules/figli/edlp_migrate/config/install/migrate_plus.migration.d6_edlp_sons.yml @@ -0,0 +1,101 @@ +id: d6_edlp_sons +label: Edlp Drupal 6 sons +migration_tags: + - Drupal 6 +deriver: Drupal\node\Plugin\migrate\D6NodeDeriver +migration_group: d6_edlp +source: + plugin: d6_edlp_sons + node_type: son +process: + # In D6, nodes always have a tnid, but it's zero for untranslated nodes. + # We normalize it to equal the nid in that case. + # @see \Drupal\node\Plugin\migrate\source\d6\Node::prepareRow(). + # If you are using this file to build a custom migration consider removing + # the nid and vid fields to allow incremental migrations. + # nid: tnid + # vid: vid + type: + plugin: default_value + default_value: autre_son + langcode: + plugin: default_value + source: language + default_value: "und" + title: + - + plugin: get + source: title + - + plugin: skip_on_empty + method: row + + uid: + - + plugin: migration_lookup + migration: d6_edlp_users + source: node_uid + - + plugin: default_value + default_value: 1 + + # status: status + created: created + changed: changed + promote: promote + sticky: sticky + revision_uid: revision_uid + revision_log: log + revision_timestamp: timestamp + + # body + 'body/value': body + 'body/format': + plugin: default_value + default_value: wysiwyg + + # field_genre -> field_genres + field_genres: + plugin: migration_lookup + migration: d6_edlp_genres + source: genres + + # field_artiste: field_locuteurs + # text -> entity ref terms + # instead of entity_generate, i create the term in prepareRow from plugin + field_locuteurs: locuteurs + + # field_compositeur: field_collectionneurs + # text -> entity ref terms + # instead of entity_generate, i create the term in prepareRow from plugin + field_collectionneurs: collectionneurs + + + # # file + # # field_mp3 -> field_son + field_son: + plugin: migration_lookup + migration: d6_edlp_allfiles + source: sons_fid + + # workflow + status: + plugin: static_map + source: workflow + map: + 1: 0 + 2: 0 + 3: 1 + 4: 1 + + + +destination: + plugin: entity:node +migration_dependencies: + required: + - d6_edlp_users + - d6_edlp_entrees + - d6_edlp_genres + - d6_edlp_langues + - d6_edlp_allfiles diff --git a/sites/all/modules/figli/edlp_migrate/src/Plugin/migrate/source/D6EdlpSons.php b/sites/all/modules/figli/edlp_migrate/src/Plugin/migrate/source/D6EdlpSons.php new file mode 100644 index 000000000..90cd6e77c --- /dev/null +++ b/sites/all/modules/figli/edlp_migrate/src/Plugin/migrate/source/D6EdlpSons.php @@ -0,0 +1,193 @@ +select('node_revisions', 'nr'); + $query->innerJoin('node', 'n', static::JOIN); + $this->handleTranslations($query); + + $query->fields('n', [ + 'nid', + 'type', + 'language', + 'status', + 'created', + 'changed', + 'comment', + 'promote', + 'moderate', + 'sticky', + 'tnid', + 'translate', + ]) + ->fields('nr', [ + 'title', + 'body', + 'teaser', + 'log', + 'timestamp', + 'format', + 'vid', + ]); + $query->addField('n', 'uid', 'node_uid'); + $query->addField('nr', 'uid', 'revision_uid'); + + // If the content_translation module is enabled, get the source langcode + // to fill the content_translation_source field. + if ($this->moduleHandler->moduleExists('content_translation')) { + $query->leftJoin('node', 'nt', 'n.tnid = nt.nid'); + $query->addField('nt', 'language', 'source_langcode'); + } + + if (isset($this->configuration['node_type'])) { + $query->condition('n.type', $this->configuration['node_type']); + } + + return $query; + } + + + /** + * {@inheritdoc} + */ + public function fields() { + + $fields = [ + 'nid' => $this->t('Node ID'), + 'type' => $this->t('Type'), + 'title' => $this->t('Title'), + 'body' => $this->t('Body'), + 'format' => $this->t('Format'), + 'teaser' => $this->t('Teaser'), + '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 prepareRow(Row $row) { + drush_print(' '); + drush_print('- - - - - - - - - - - - - - - - - - - - - - - -'); + drush_print($row->getSourceProperty('title')); + + + // format = 0 can happen when the body field is hidden. Set the format to 1 + // to avoid migration map issues (since the body field isn't used anyway). + if ($row->getSourceProperty('format') === '0') { + $row->setSourceProperty('format', $this->filterDefaultFormat); + } + + if ($this->moduleExists('content') && $this->getModuleSchemaVersion('content') >= 6001) { + foreach ($this->getFieldValues($row) as $field => $values) { + $row->setSourceProperty($field, $values); + } + } + $row_nid = $row->getSourceProperty('nid'); + + + // Make sure we always have a translation set. + if ($row->getSourceProperty('tnid') == 0) { + $row->setSourceProperty('tnid', $row_nid); + } + + // Genres + $genres = []; + foreach ($row->getSourceProperty('field_genre') as $key => $value) { + $genres[] = array($value['value']); + } + $row->setSourceProperty('genres', $genres); + + // locuteurs + $locuteurs = []; + foreach ($row->getSourceProperty('field_artiste') as $key => $value) { + $term_name = $value['value']; + // check if term already exists in configured vocabulary + $term = array_shift(taxonomy_term_load_multiple_by_name($term_name, 'locuteurs')); + if(!$term){ + // if not create the term and get the tid + $term = \Drupal\taxonomy\Entity\Term::create([ + 'vid' => 'locuteurs', + 'name' => $term_name, + ]); + $term->save(); + } + // record the tid in array + $locuteurs[] = $term->get('tid')->value; + } + $row->setSourceProperty('locuteurs', $locuteurs); + + // collectionneurs + $collectionneurs = []; + foreach ($row->getSourceProperty('field_compositeur') as $key => $value) { + $term_name = $value['value']; + // check if term already exists in configured vocabulary + $term = array_shift(taxonomy_term_load_multiple_by_name($term_name, 'collectionneurs')); + if(!$term){ + // if not create the term and get the tid + $term = \Drupal\taxonomy\Entity\Term::create([ + 'vid' => 'collectionneurs', + 'name' => $term_name, + ]); + $term->save(); + } + // record the tid in array + $collectionneurs[] = $term->get('tid')->value; + } + $row->setSourceProperty('collectionneurs', $collectionneurs); + + // son + // drush_print('field_mp3 : ', 0, null, false); + // drush_print_r($row->getSourceProperty('field_mp3')); + $sons = []; + foreach ($row->getSourceProperty('field_mp3') as $key => $value) { + $sons[] = array($value['fid']); + } + $row->setSourceProperty('sons_fid', $sons); + + // get the workflow state (will be converted on status) + $query = $this->select('workflow_node', 'wn'); + $query->fields('wn', ['sid']); + $query->condition('wn.nid', $row_nid); + $results = $query->execute()->fetchField(); + + // drush_print('workflow query results : ', 0, null, false); + // drush_print_r($results); + + $row->setSourceProperty('workflow', $results); + + + $row = parent::prepareRow($row); + return $row; + } + + +}