From 4f9facddf1332b3e24bc68a5903e8894b83c029f Mon Sep 17 00:00:00 2001 From: bach Date: Tue, 16 Feb 2021 16:50:22 +0100 Subject: [PATCH] first draft of flaglist migrate, completly screwed up --- ...igrate_plus.migration.d7_flaglistitems.yml | 48 +++++++ .../migrate_plus.migration.d7_flaglists.yml | 45 +++++++ .../materio_migrate/materio_migrate.info.yml | 4 +- .../migrate/source/D7FlagListItemsMaterio.php | 123 ++++++++++++++++++ .../migrate/source/D7FlagListsMaterio.php | 123 ++++++++++++++++++ 5 files changed, 342 insertions(+), 1 deletion(-) create mode 100644 web/modules/custom/materio_migrate/config/install/migrate_plus.migration.d7_flaglistitems.yml create mode 100644 web/modules/custom/materio_migrate/config/install/migrate_plus.migration.d7_flaglists.yml create mode 100644 web/modules/custom/materio_migrate/src/Plugin/migrate/source/D7FlagListItemsMaterio.php create mode 100644 web/modules/custom/materio_migrate/src/Plugin/migrate/source/D7FlagListsMaterio.php diff --git a/web/modules/custom/materio_migrate/config/install/migrate_plus.migration.d7_flaglistitems.yml b/web/modules/custom/materio_migrate/config/install/migrate_plus.migration.d7_flaglistitems.yml new file mode 100644 index 00000000..c1d018e8 --- /dev/null +++ b/web/modules/custom/materio_migrate/config/install/migrate_plus.migration.d7_flaglistitems.yml @@ -0,0 +1,48 @@ +# This is the migrations file from Flag Lists D7 +id: d7_flaglistitems +label: Materio Flag List Items D7 +migration_group: d7_materio +#audit: true +migration_tags: + - Drupal 7 + - Content +migration_dependencies: + required: + - d7_flaglists + - d7_users + - d7_node_article + - d7_node_materiau +source: + plugin: d7_flaglistitems_materio +# +process: + id: fcid + # user_id: uid + user_id: + plugin: migration_lookup + migration: d7_users + source: uid + + name: + plugin: concat + source: + - title + - entity_id + delimiter: ' ' + baseflag: relatedflag + flag_list: fid + # entity_id: entity_id + entity_id: + - + plugin: skip_on_empty + method: process + source: linked_materials + - + plugin: migration_lookup + migration: d7_node_materiau + no_stub: true + + # type: entity_type +# +destination: + plugin: entity:flag_list_item diff --git a/web/modules/custom/materio_migrate/config/install/migrate_plus.migration.d7_flaglists.yml b/web/modules/custom/materio_migrate/config/install/migrate_plus.migration.d7_flaglists.yml new file mode 100644 index 00000000..375138b9 --- /dev/null +++ b/web/modules/custom/materio_migrate/config/install/migrate_plus.migration.d7_flaglists.yml @@ -0,0 +1,45 @@ +# This is the migrations file from Flag Lists D7 +id: d7_flaglists +label: 'Materio Flagging Collections D7' +migration_group: d7_materio +audit: true +migration_tags: + - Drupal 7 + - Content + - materio +migration_dependencies: + required: + - d7_users + - d7_node_article + - d7_node_materiau +source: + plugin: d7_flaglists_materio + # key: migrate +# +process: + id: fid + vid: fid + langcode: + plugin: default_value + default_value: en + # needs migration_lookup + # user_id: uid + user_id: + plugin: migration_lookup + migration: d7_users + source: uid + + name: title + # Shall be name from flag + # templateflag: name + # relatedflag: name + templateflag: + plugin: default_value + default_value: dossier + relatedflag: + plugin: default_value + default_value: dossier +# +destination: + plugin: entity:flagging_collection + default_bundle: dossier diff --git a/web/modules/custom/materio_migrate/materio_migrate.info.yml b/web/modules/custom/materio_migrate/materio_migrate.info.yml index a3f9fda5..2140ca2c 100644 --- a/web/modules/custom/materio_migrate/materio_migrate.info.yml +++ b/web/modules/custom/materio_migrate/materio_migrate.info.yml @@ -12,7 +12,7 @@ dependencies: - migrate_tools:migrate_tools # - drupal:field_group_migrate - telephone_validation - + # https://www.liip.ch/en/blog/using-the-new-drupal-8-migration-api-module config_devel: install: @@ -33,3 +33,5 @@ config_devel: - migrate_plus.migration.d7_taxonomy_term_tags - migrate_plus.migration.d7_taxonomy_term_thesaurus_i18n - migrate_plus.migration.d7_taxonomy_term_thesaurus + - migrate_plus.migration.d7_flaglists + - migrate_plus.migration.d7_flaglistitems diff --git a/web/modules/custom/materio_migrate/src/Plugin/migrate/source/D7FlagListItemsMaterio.php b/web/modules/custom/materio_migrate/src/Plugin/migrate/source/D7FlagListItemsMaterio.php new file mode 100644 index 00000000..b9b38a83 --- /dev/null +++ b/web/modules/custom/materio_migrate/src/Plugin/migrate/source/D7FlagListItemsMaterio.php @@ -0,0 +1,123 @@ +select('flag_lists_content', 'c'); + $query->join('flag_lists_flags', 'f', 'c.fid = f.fid'); + $query->fields('c', [ + 'fcid', + 'fid', + 'entity_type', + 'entity_id', + 'uid', + 'sid', + 'timestamp', + ]) + ->fields('f', [ + 'name', + 'title', + ]); + return $query; + } + + /** + * {@inheritdoc} + */ + public function fields() { + $fields = [ + 'fcid' => $this->t('Flag content id'), + 'fid' => $this->t('Flag lists id #'), + 'entity_type' => $this->t('Entity type'), + 'entity_id' => $this->t('Entity #'), + 'uid' => $this->t('Owner'), + 'sid' => $this->t('Sid'), + 'timestamp' => $this->t('Timestamp'), + ]; + return $fields; + } + + /** + * {@inheritdoc} + */ + public function getIds() { + return [ + 'fcid' => [ + 'type' => 'integer', + 'alias' => 'f', + ], + ]; + } + + /** + * {@inheritdoc} + */ + public function prepareRow(Row $row) { + + $messenger = \Drupal::messenger(); + $logger = \Drupal::logger('flag_lists'); + + // // Check and get the user name. + // $uid = $row->getSourceProperty('uid'); + // $user = User::load($uid); + // if (!empty($user)) { + // $owner = $uid; + // } + // else { + // $owner = 1; + // } + // $row->setSourceProperty('uid', $owner); + + // Check if the flagging collection exist. + $found = FALSE; + $flagListsService = \Drupal::service('flaglists'); + $baseFlags = $flagListsService->getAllFlaggingCollections(); + foreach ($baseFlags as $flag) { + if ($found = + $flag->get('id')->value == $row->getSourceProperty('fid')) { + $relatedFlag = $flag->get('relatedflag')->getValue(); + $relatedFlagList = $relatedFlag['0']['target_id']; + $row->setSourceProperty('relatedflag', $relatedFlagList); + break; + } + } + if (!$found) { + $message = $this->t('The flagging collection "@collection" wasn\'t found'); + $messenger->addError($message, + ['@collection' => $row->getSourceProperty('relatedflag')]); + $logger->error($message, + ['@collection' => $row->getSourceProperty('relatedflag')]); + } + + // Check if the entity exists. + // $entity_id = $row->getSourceProperty('entity_id'); + // $entity = \Drupal::entityTypeManager()->getStorage($row->getSourceProperty('entity_type'))->load($entity_id); + // if (empty($entity)) { + // $message = $this->t('The entity with ID "@entity_id" wasn\'t found', + // ['@entity_id' => $entity_id]); + // $messenger->addError($message); + // $logger->error($message); + // } + + return parent::prepareRow($row); + } + +} diff --git a/web/modules/custom/materio_migrate/src/Plugin/migrate/source/D7FlagListsMaterio.php b/web/modules/custom/materio_migrate/src/Plugin/migrate/source/D7FlagListsMaterio.php new file mode 100644 index 00000000..7a00a78c --- /dev/null +++ b/web/modules/custom/materio_migrate/src/Plugin/migrate/source/D7FlagListsMaterio.php @@ -0,0 +1,123 @@ +select('flag_lists_flags', 'c'); + $query->join('flag', 'f', 'c.pfid = f.fid'); + $query->fields('c', [ + 'fid', + 'pfid', + 'uid', + 'entity_type', + 'title', + 'options', + ]) + ->fields('f', [ + 'name', + ]); + return $query; + } + + /** + * {@inheritdoc} + */ + public function fields() { + $fields = [ + 'fid' => $this->t('Flag List #'), + 'pfid' => $this->t('Parent flag id #'), + 'uid' => $this->t('Owner'), + 'entity_type' => $this->t('Entity type'), + 'name' => $this->t('Machine name of the related flag'), + 'title' => $this->t('Name of flag list'), + 'option' => $this->t('Serielized info'), + ]; + return $fields; + } + + /** + * {@inheritdoc} + */ + public function getIds() { + return [ + 'fid' => [ + 'type' => 'integer', + 'alias' => 'c', + ], + ]; + } + + /** + * {@inheritdoc} + */ + public function prepareRow(Row $row) { + + # WE GOT THE USER FROM LOOKUP PLUGIN + // Check if the user exists. + // $uid = $row->getSourceProperty('uid'); + // $user = User::load($uid); + // if (!empty($user)) { + // $owner = $uid; + // } + // else { + // // Make the Administrator the owner. + // $owner = 1; + // } + // $row->setSourceProperty('uid', $owner); + + # WE SET THE FLAG TEMPLATE DEFAULT "DOSSIER" + // Check if the template flag exist. + // $found = FALSE; + // $flagService = \Drupal::service('flag'); + // $templateFlags = $flagService->getAllFlags( + // $row->getSourceProperty('entity_type')); + // foreach ($templateFlags as $flag) { + // if ($found = + // $flag->get('id') == $row->getSourceProperty('name')) { + // break; + // } + // } + // if (!$found) { + // $message = $this->t('The template flag "@flag" wasn\'t found. Using fallback.', + // ['@flag' => $row->getSourceProperty('name')]); + // $messenger = \Drupal::messenger(); + // $logger = \Drupal::logger('flag_lists'); + // $messenger->addWarning($message); + // $logger->warning($message); + // + // // Fall back to known existing flag. + // $row->setSourceProperty('name', 'flag_list_template_1'); + // } + + // [error] Drupal\Core\Config\Entity\Exception\ConfigEntityIdLengthException: Attempt to create a bundle with an ID longer than 32 characters: prototypage_rapide_grandes_dimensions(). in Drupal\Core\Entity\EntityBase->preSave() (line 439 of /var/www/html/d8.materio.com/public_html/web/core/lib/Drupal/Core/Entity/EntityBase.php). + // [error] Attempt to create a bundle with an ID longer than 32 characters: prototypage_rapide_grandes_dimensions(). (/var/www/html/d8.materio.com/public_html/web/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php:846) + + // limit folder name to 32 characters + $title = $row->getSourceProperty('title'); + if (strlen($title) > 32) { + $title = substr($title, 0, 29).'...'; + $title = $row->setSourceProperty('title', $title); + } + + return parent::prepareRow($row); + } + +}