Browse Source

first draft of flaglist migrate, completly screwed up

bach 3 years ago
parent
commit
4f9facddf1

+ 48 - 0
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

+ 45 - 0
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

+ 3 - 1
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

+ 123 - 0
web/modules/custom/materio_migrate/src/Plugin/migrate/source/D7FlagListItemsMaterio.php

@@ -0,0 +1,123 @@
+<?php
+
+namespace Drupal\materio_migrate\Plugin\migrate\source;
+
+use Drupal\user\Entity\User;
+use Drupal\migrate\Plugin\migrate\source\SqlBase;
+use Drupal\migrate\Row;
+
+/**
+ * Minimalistic example for a SqlBase source plugin.
+ *
+ * @MigrateSource(
+ *   id = "d7_flaglistitems_materio",
+ *   source_module = "flag_lists"
+ * )
+ */
+class D7FlagListItemsMaterio extends SqlBase {
+
+  /**
+   * {@inheritdoc}
+   */
+  public function query() {
+    // Source data is queried from 'curling_games' table.
+    $query = $this->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);
+  }
+
+}

+ 123 - 0
web/modules/custom/materio_migrate/src/Plugin/migrate/source/D7FlagListsMaterio.php

@@ -0,0 +1,123 @@
+<?php
+
+namespace Drupal\materio_migrate\Plugin\migrate\source;
+
+use Drupal\migrate\Plugin\migrate\source\SqlBase;
+use Drupal\migrate\Row;
+use Drupal\user\Entity\User;
+
+/**
+ * Minimalistic example for a SqlBase source plugin.
+ *
+ * @MigrateSource(
+ *   id = "d7_flaglists_materio",
+ *   source_module = "flag_lists",
+ * )
+ */
+class D7FlagListsMaterio extends SqlBase {
+
+  /**
+   * {@inheritdoc}
+   */
+  public function query() {
+    // Source data is queried from 'flag_lists_flags' table.
+    $query = $this->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);
+  }
+
+}