fixe user company bug
This commit is contained in:
		@@ -32,7 +32,7 @@ process:
 | 
			
		||||
      source: parent
 | 
			
		||||
    -
 | 
			
		||||
      plugin: migration_lookup
 | 
			
		||||
      migration: d7_taxonomy_term_companie
 | 
			
		||||
      migration: d7_taxonomy_term_company
 | 
			
		||||
  parent:
 | 
			
		||||
    plugin: default_value
 | 
			
		||||
    default_value: 0
 | 
			
		||||
 
 | 
			
		||||
@@ -6,19 +6,14 @@ migration_tags:
 | 
			
		||||
  - Drupal 7
 | 
			
		||||
  - Content
 | 
			
		||||
  - Materio
 | 
			
		||||
class: Drupal\user\Plugin\migrate\User
 | 
			
		||||
 | 
			
		||||
source:
 | 
			
		||||
  plugin: d7_user
 | 
			
		||||
  plugin: d7_users
 | 
			
		||||
 | 
			
		||||
destination:
 | 
			
		||||
  plugin: entity:user
 | 
			
		||||
 | 
			
		||||
process:
 | 
			
		||||
  # If you are using this file to build a custom migration consider removing
 | 
			
		||||
  # the uid field to allow incremental migrations.
 | 
			
		||||
  # uid: uid
 | 
			
		||||
 | 
			
		||||
  name: name
 | 
			
		||||
  pass: pass
 | 
			
		||||
  mail:
 | 
			
		||||
@@ -53,10 +48,6 @@ process:
 | 
			
		||||
    source: language
 | 
			
		||||
    fallback_to_site_default: true
 | 
			
		||||
  init: init
 | 
			
		||||
  # roles:
 | 
			
		||||
  #   plugin: migration_lookup
 | 
			
		||||
  #   migration: d7_user_role
 | 
			
		||||
  #   source: roles
 | 
			
		||||
  roles:
 | 
			
		||||
    plugin: static_map
 | 
			
		||||
    source: roles
 | 
			
		||||
@@ -76,29 +67,43 @@ process:
 | 
			
		||||
      12: "translator"
 | 
			
		||||
      13: "admin_showroom"
 | 
			
		||||
 | 
			
		||||
  user_picture:
 | 
			
		||||
    -
 | 
			
		||||
      plugin: default_value
 | 
			
		||||
      source: picture
 | 
			
		||||
      default_value: null
 | 
			
		||||
    -
 | 
			
		||||
      plugin: migration_lookup
 | 
			
		||||
      migration: d7_file
 | 
			
		||||
 | 
			
		||||
  # TODO : a vérifier
 | 
			
		||||
  # j’ai fait un test de recherche sur ce tableau user (recherche par email "@materio.com"), les réponses sont pour l’instant aberrantes (kevin travaille par exemple chez Onip Peinture)
 | 
			
		||||
  field_company:
 | 
			
		||||
    plugin: migration_lookup
 | 
			
		||||
    migration: d7_taxonomy_term_company
 | 
			
		||||
    source: field_company
 | 
			
		||||
    no_stub: true
 | 
			
		||||
  # field_company:
 | 
			
		||||
  #   -
 | 
			
		||||
  #     plugin: sub_process
 | 
			
		||||
  #     source: field_company
 | 
			
		||||
  #     process:
 | 
			
		||||
  #       target_id:
 | 
			
		||||
  #         plugin: migration_lookup
 | 
			
		||||
  #         migration: d7_taxonomy_term_company
 | 
			
		||||
  #         source: tid
 | 
			
		||||
  #         no_stub: true
 | 
			
		||||
  # field_company:
 | 
			
		||||
  #   plugin: default_value
 | 
			
		||||
  #   default_value: 2673
 | 
			
		||||
 | 
			
		||||
  field_showroom:
 | 
			
		||||
    plugin: migration_lookup
 | 
			
		||||
    migration: d7_taxonomy_term_showroom
 | 
			
		||||
    no_stub: true
 | 
			
		||||
    source: field_showroom
 | 
			
		||||
 | 
			
		||||
  field_memo: field_memo
 | 
			
		||||
 | 
			
		||||
  # user_picture:
 | 
			
		||||
  #   -
 | 
			
		||||
  #     plugin: default_value
 | 
			
		||||
  #     source: picture
 | 
			
		||||
  #     default_value: null
 | 
			
		||||
  #   -
 | 
			
		||||
  #     plugin: migration_lookup
 | 
			
		||||
  #     migration: d7_file
 | 
			
		||||
  #     no_stub: true
 | 
			
		||||
 | 
			
		||||
migration_dependencies:
 | 
			
		||||
  required:
 | 
			
		||||
    - d7_allpublicfiles
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,152 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace Drupal\materio_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', 0, '>');
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * {@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',
 | 
			
		||||
      ],
 | 
			
		||||
    ];
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user