D7Users.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace Drupal\materio_migrate\Plugin\migrate\source;
  3. use Drupal\migrate\Row;
  4. use Drupal\migrate_drupal\Plugin\migrate\source\d7\FieldableEntity;
  5. /**
  6. * Drupal 7 user source from database.
  7. *
  8. * @MigrateSource(
  9. * id = "d7_users",
  10. * source_module = "user"
  11. * )
  12. */
  13. class D7Users extends FieldableEntity {
  14. /**
  15. * {@inheritdoc}
  16. */
  17. public function query() {
  18. return $this->select('users', 'u')
  19. ->fields('u')
  20. ->condition('u.uid', 1, '>');
  21. }
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public function fields() {
  26. $fields = [
  27. 'uid' => $this->t('User ID'),
  28. 'name' => $this->t('Username'),
  29. 'pass' => $this->t('Password'),
  30. 'mail' => $this->t('Email address'),
  31. 'signature' => $this->t('Signature'),
  32. 'signature_format' => $this->t('Signature format'),
  33. 'created' => $this->t('Registered timestamp'),
  34. 'access' => $this->t('Last access timestamp'),
  35. 'login' => $this->t('Last login timestamp'),
  36. 'status' => $this->t('Status'),
  37. 'timezone' => $this->t('Timezone'),
  38. 'language' => $this->t('Language'),
  39. 'picture' => $this->t('Picture'),
  40. 'init' => $this->t('Init'),
  41. 'data' => $this->t('User data'),
  42. 'roles' => $this->t('Roles'),
  43. ];
  44. // Profile fields.
  45. if ($this->moduleExists('profile')) {
  46. $fields += $this->select('profile_fields', 'pf')
  47. ->fields('pf', ['name', 'title'])
  48. ->execute()
  49. ->fetchAllKeyed();
  50. }
  51. return $fields;
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public function prepareRow(Row $row) {
  57. $uid = $row->getSourceProperty('uid');
  58. $roles = $this->select('users_roles', 'ur')
  59. ->fields('ur', ['rid'])
  60. ->condition('ur.uid', $uid)
  61. ->execute()
  62. ->fetchCol();
  63. $row->setSourceProperty('roles', $roles);
  64. $row->setSourceProperty('data', unserialize($row->getSourceProperty('data')));
  65. // If this entity was translated using Entity Translation, we need to get
  66. // its source language to get the field values in the right language.
  67. // The translations will be migrated by the d7_user_entity_translation
  68. // migration.
  69. $entity_translatable = $this->isEntityTranslatable('user');
  70. $source_language = $this->getEntityTranslationSourceLanguage('user', $uid);
  71. $language = $entity_translatable && $source_language ? $source_language : $row->getSourceProperty('language');
  72. $row->setSourceProperty('entity_language', $language);
  73. // Get Field API field values.
  74. foreach ($this->getFields('user') as $field_name => $field) {
  75. // Ensure we're using the right language if the entity and the field are
  76. // translatable.
  77. $field_language = $entity_translatable && $field['translatable'] ? $language : NULL;
  78. $field_values = $this->getFieldValues('user', $field_name, $uid, NULL, $field_language);
  79. // $field_values = [];
  80. // if ($field_name === 'field_company') {
  81. // // print($uid . " ---\n");
  82. // // print_r($tmp_field_values);
  83. // // fix the taxo tid as tode module replace it by node nid
  84. // // get the real tode tid
  85. // foreach ($tmp_field_values as $key => $value) {
  86. // $tode_tid = $this->select('field_data_field_tode_company', 'tode')
  87. // ->condition('entity_type', 'node')
  88. // ->condition('entity_id', $value['tid'])
  89. // ->fields('tode', ['field_tode_company_tid'])
  90. // ->execute()->fetchField();
  91. // $field_values[] = array('tid'=>$tode_tid);
  92. // }
  93. // // print_r($field_values);
  94. // $row->setSourceProperty('companies', $field_values);
  95. // }
  96. $row->setSourceProperty($field_name, $field_values);
  97. }
  98. // Get profile field values. This code is lifted directly from the D6
  99. // ProfileFieldValues plugin.
  100. if ($this->getDatabase()->schema()->tableExists('profile_value')) {
  101. $query = $this->select('profile_value', 'pv')
  102. ->fields('pv', ['fid', 'value']);
  103. $query->leftJoin('profile_field', 'pf', 'pf.fid=pv.fid');
  104. $query->fields('pf', ['name', 'type']);
  105. $query->condition('uid', $row->getSourceProperty('uid'));
  106. $results = $query->execute();
  107. foreach ($results as $profile_value) {
  108. if ($profile_value['type'] == 'date') {
  109. $date = unserialize($profile_value['value']);
  110. $date = date('Y-m-d', mktime(0, 0, 0, $date['month'], $date['day'], $date['year']));
  111. $row->setSourceProperty($profile_value['name'], ['value' => $date]);
  112. }
  113. elseif ($profile_value['type'] == 'list') {
  114. // Explode by newline and comma.
  115. $row->setSourceProperty($profile_value['name'], preg_split("/[\r\n,]+/", $profile_value['value']));
  116. }
  117. else {
  118. $row->setSourceProperty($profile_value['name'], [$profile_value['value']]);
  119. }
  120. }
  121. }
  122. return parent::prepareRow($row);
  123. }
  124. /**
  125. * {@inheritdoc}
  126. */
  127. public function getIds() {
  128. return [
  129. 'uid' => [
  130. 'type' => 'integer',
  131. 'alias' => 'u',
  132. ],
  133. ];
  134. }
  135. }