Browse Source

popsu_migrate: d7_users ok

bach 3 years ago
parent
commit
4702202d07

+ 1 - 1
config/sync/migrate_plus.migration.d7_allpublicfiles.yml

@@ -1,4 +1,4 @@
-uuid: f115618c-45ec-4c30-b20e-5b36c2494766
+uuid: 0f97d872-fa48-4944-81a8-90265be2094e
 langcode: fr
 status: true
 dependencies: {  }

+ 64 - 0
config/sync/migrate_plus.migration.d7_users.yml

@@ -0,0 +1,64 @@
+uuid: 17350b9c-18b6-4716-b944-c09e2dd67163
+langcode: fr
+status: true
+dependencies: {  }
+_core:
+  default_config_hash: 7HNGRfzA6vYrhJVsS7Wm-d_g6sucSx6c_K48dpghcks
+id: d7_users
+class: null
+field_plugin_method: null
+cck_plugin_method: null
+migration_tags:
+  - 'Drupal 7'
+  - Content
+  - Popsu
+migration_group: popsu
+label: 'User accounts'
+source:
+  plugin: d7_users
+  batch_size: 1000
+process:
+  name: name
+  pass: pass
+  mail:
+    -
+      plugin: skip_on_value
+      equals: true
+      source: mail
+      method: row
+      value:
+        - manager-popsu@renaud-cuny.com
+    -
+      plugin: default_value
+      source: mail
+      default_value: null
+  created: created
+  access: access
+  login: login
+  status: status
+  timezone: timezone
+  init: init
+  roles:
+    -
+      plugin: skip_on_value
+      equals: true
+      source: roles
+      method: row
+      value:
+        - 5
+        - 3
+    -
+      plugin: static_map
+      source: roles
+      default_value: 2
+      map:
+        1: anonymous
+        2: authenticated
+        3: admin
+        4: editeur
+        5: root
+destination:
+  plugin: 'entity:user'
+migration_dependencies:
+  required:
+    - d7_allpublicfiles

+ 1 - 1
config/sync/migrate_plus.migration_group.popsu.yml

@@ -1,4 +1,4 @@
-uuid: c7d02c39-423d-4322-b600-2c9a60deb99a
+uuid: 325cb8a6-0ecb-4e58-9248-cc86c51e0047
 langcode: en
 status: true
 dependencies:

+ 74 - 0
web/modules/custom/popsu_migrate/config/install/migrate_plus.migration.d7_users.yml

@@ -0,0 +1,74 @@
+id: d7_users
+label: User accounts
+migration_group: popsu
+audit: true
+migration_tags:
+  - Drupal 7
+  - Content
+  - Popsu
+
+source:
+  plugin: d7_users
+  batch_size: 1000
+
+destination:
+  plugin: entity:user
+
+process:
+  name: name
+  pass: pass
+  mail:
+    -
+      plugin: skip_on_value
+      equals: true
+      source: mail
+      method: row
+      value:
+        - "manager-popsu@renaud-cuny.com"
+        # - "editeur-popsu@renaud-cuny.com"
+    -
+      plugin: default_value
+      source: mail
+      default_value: null
+
+  created: created
+  access: access
+  login: login
+  status: status
+  timezone: timezone
+  # langcode:
+  #   plugin: user_langcode
+  #   source: entity_language
+  #   fallback_to_site_default: false
+  # preferred_langcode:
+  #   plugin: user_langcode
+  #   source: language
+  #   fallback_to_site_default: true
+  # preferred_admin_langcode:
+  #   plugin: user_langcode
+  #   source: language
+  #   fallback_to_site_default: true
+  init: init
+  roles:
+    -
+      plugin: skip_on_value
+      equals: true
+      source: roles
+      method: row
+      value:
+        - 5
+        - 3
+    -
+      plugin: static_map
+      source: roles
+      default_value: 2
+      map:
+        1: "anonymous"
+        2: "authenticated"
+        3: "admin"
+        4: "editeur"
+        5: "root"
+
+migration_dependencies:
+  required:
+    - d7_allpublicfiles

+ 152 - 0
web/modules/custom/popsu_migrate/src/Plugin/migrate/source/D7Users.php

@@ -0,0 +1,152 @@
+<?php
+
+namespace Drupal\popsu_migrate\Plugin\migrate\source;
+
+use Drupal\migrate\Row;
+use Drupal\migrate_drupal\Plugin\migrate\source\d7\FieldableEntity;
+
+/**
+ * Drupal 7 user source from database.
+ *
+ * @MigrateSource(
+ *   id = "d7_users",
+ *   source_module = "user"
+ * )
+ */
+class D7Users extends FieldableEntity {
+
+  /**
+   * {@inheritdoc}
+   */
+  public function query() {
+    return $this->select('users', 'u')
+      ->fields('u')
+      ->condition('u.uid', 1, '>');
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function fields() {
+    $fields = [
+      'uid' => $this->t('User ID'),
+      'name' => $this->t('Username'),
+      'pass' => $this->t('Password'),
+      'mail' => $this->t('Email address'),
+      'signature' => $this->t('Signature'),
+      'signature_format' => $this->t('Signature format'),
+      'created' => $this->t('Registered timestamp'),
+      'access' => $this->t('Last access timestamp'),
+      'login' => $this->t('Last login timestamp'),
+      'status' => $this->t('Status'),
+      'timezone' => $this->t('Timezone'),
+      'language' => $this->t('Language'),
+      'picture' => $this->t('Picture'),
+      'init' => $this->t('Init'),
+      'data' => $this->t('User data'),
+      'roles' => $this->t('Roles'),
+    ];
+
+    // Profile fields.
+    if ($this->moduleExists('profile')) {
+      $fields += $this->select('profile_fields', 'pf')
+        ->fields('pf', ['name', 'title'])
+        ->execute()
+        ->fetchAllKeyed();
+    }
+
+    return $fields;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function prepareRow(Row $row) {
+    $uid = $row->getSourceProperty('uid');
+
+    $roles = $this->select('users_roles', 'ur')
+      ->fields('ur', ['rid'])
+      ->condition('ur.uid', $uid)
+      ->execute()
+      ->fetchCol();
+    $row->setSourceProperty('roles', $roles);
+
+    $row->setSourceProperty('data', unserialize($row->getSourceProperty('data')));
+
+    // If this entity was translated using Entity Translation, we need to get
+    // its source language to get the field values in the right language.
+    // The translations will be migrated by the d7_user_entity_translation
+    // migration.
+    // $entity_translatable = $this->isEntityTranslatable('user');
+    // $source_language = $this->getEntityTranslationSourceLanguage('user', $uid);
+    // $language = $entity_translatable && $source_language ? $source_language : $row->getSourceProperty('language');
+    // $row->setSourceProperty('entity_language', $language);
+    //
+    // // Get Field API field values.
+    // foreach ($this->getFields('user') as $field_name => $field) {
+    //   // Ensure we're using the right language if the entity and the field are
+    //   // translatable.
+    //   $field_language = $entity_translatable && $field['translatable'] ? $language : NULL;
+    //   $field_values = $this->getFieldValues('user', $field_name, $uid, NULL, $field_language);
+    //   // $field_values = [];
+    //   // if ($field_name === 'field_company') {
+    //   //   // print($uid . " ---\n");
+    //   //   // print_r($tmp_field_values);
+    //   //   // fix the taxo tid as tode module replace it by node nid
+    //   //   // get the real tode tid
+    //   //   foreach ($tmp_field_values as $key => $value) {
+    //   //     $tode_tid = $this->select('field_data_field_tode_company', 'tode')
+    //   //     ->condition('entity_type', 'node')
+    //   //     ->condition('entity_id', $value['tid'])
+    //   //     ->fields('tode', ['field_tode_company_tid'])
+    //   //     ->execute()->fetchField();
+    //   //     $field_values[] = array('tid'=>$tode_tid);
+    //   //   }
+    //   //   // print_r($field_values);
+    //   //   $row->setSourceProperty('companies', $field_values);
+    //   // }
+    //   $row->setSourceProperty($field_name, $field_values);
+    // }
+
+    // // Get profile field values. This code is lifted directly from the D6
+    // // ProfileFieldValues plugin.
+    // if ($this->getDatabase()->schema()->tableExists('profile_value')) {
+    //   $query = $this->select('profile_value', 'pv')
+    //     ->fields('pv', ['fid', 'value']);
+    //   $query->leftJoin('profile_field', 'pf', 'pf.fid=pv.fid');
+    //   $query->fields('pf', ['name', 'type']);
+    //   $query->condition('uid', $row->getSourceProperty('uid'));
+    //   $results = $query->execute();
+    //
+    //   foreach ($results as $profile_value) {
+    //     if ($profile_value['type'] == 'date') {
+    //       $date = unserialize($profile_value['value']);
+    //       $date = date('Y-m-d', mktime(0, 0, 0, $date['month'], $date['day'], $date['year']));
+    //       $row->setSourceProperty($profile_value['name'], ['value' => $date]);
+    //     }
+    //     elseif ($profile_value['type'] == 'list') {
+    //       // Explode by newline and comma.
+    //       $row->setSourceProperty($profile_value['name'], preg_split("/[\r\n,]+/", $profile_value['value']));
+    //     }
+    //     else {
+    //       $row->setSourceProperty($profile_value['name'], [$profile_value['value']]);
+    //     }
+    //   }
+    // }
+
+    return parent::prepareRow($row);
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function getIds() {
+    return [
+      'uid' => [
+        'type' => 'integer',
+        'alias' => 'u',
+      ],
+    ];
+  }
+
+}