D6EdlpUsers.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace Drupal\edlp_migrate\Plugin\migrate\source;
  3. use Drupal\migrate\Row;
  4. use Drupal\user\Plugin\migrate\source\d6\User as D6User;
  5. /**
  6. * Source plugin for edlp user accounts.
  7. *
  8. * @MigrateSource(
  9. * id = "d6_edlp_users"
  10. * )
  11. */
  12. class D6EdlpUsers extends D6User {
  13. /**
  14. * {@inheritdoc}
  15. */
  16. public function query() {
  17. return $this->select('users', 'u')
  18. ->fields('u', array_keys($this->baseFields()))
  19. ->condition('u.uid', 0, '>');
  20. }
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public function fields() {
  25. $fields = $this->baseFields();
  26. // Add roles field.
  27. $fields['roles'] = $this->t('Roles');
  28. // Profile fields.
  29. if ($this->moduleExists('profile')) {
  30. $fields += $this->select('profile_fields', 'pf')
  31. ->fields('pf', ['name', 'title'])
  32. ->execute()
  33. ->fetchAllKeyed();
  34. }
  35. return $fields;
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function prepareRow(Row $row) {
  41. // User roles.
  42. $roles = $this->select('users_roles', 'ur')
  43. ->fields('ur', ['rid'])
  44. ->condition('ur.uid', $row->getSourceProperty('uid'))
  45. ->execute()
  46. ->fetchCol();
  47. $row->setSourceProperty('roles', $roles);
  48. // We are adding here the Event contributed module column.
  49. // @see https://api.drupal.org/api/drupal/modules%21user%21user.install/function/user_update_7002/7
  50. if ($row->hasSourceProperty('timezone_id') && $row->getSourceProperty('timezone_id')) {
  51. if ($this->getDatabase()->schema()->tableExists('event_timezones')) {
  52. $event_timezone = $this->select('event_timezones', 'e')
  53. ->fields('e', ['name'])
  54. ->condition('e.timezone', $row->getSourceProperty('timezone_id'))
  55. ->execute()
  56. ->fetchField();
  57. if ($event_timezone) {
  58. $row->setSourceProperty('event_timezone', $event_timezone);
  59. }
  60. }
  61. }
  62. // Unserialize Data.
  63. $row->setSourceProperty('data', unserialize($row->getSourceProperty('data')));
  64. return parent::prepareRow($row);
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. public function getIds() {
  70. return [
  71. 'uid' => [
  72. 'type' => 'integer',
  73. 'alias' => 'u',
  74. ],
  75. ];
  76. }
  77. /**
  78. * Returns the user base fields to be migrated.
  79. *
  80. * @return array
  81. * Associative array having field name as key and description as value.
  82. */
  83. protected function baseFields() {
  84. $fields = [
  85. 'uid' => $this->t('User ID'),
  86. 'name' => $this->t('Username'),
  87. 'pass' => $this->t('Password'),
  88. 'mail' => $this->t('Email address'),
  89. 'theme' => $this->t('Theme'),
  90. 'signature' => $this->t('Signature'),
  91. 'signature_format' => $this->t('Signature format'),
  92. 'created' => $this->t('Registered timestamp'),
  93. 'access' => $this->t('Last access timestamp'),
  94. 'login' => $this->t('Last login timestamp'),
  95. 'status' => $this->t('Status'),
  96. 'timezone' => $this->t('Timezone'),
  97. 'language' => $this->t('Language'),
  98. 'picture' => $this->t('Picture'),
  99. 'init' => $this->t('Init'),
  100. 'data' => $this->t('User data'),
  101. ];
  102. // Possible field added by Date contributed module.
  103. // @see https://api.drupal.org/api/drupal/modules%21user%21user.install/function/user_update_7002/7
  104. if ($this->getDatabase()->schema()->fieldExists('users', 'timezone_name')) {
  105. $fields['timezone_name'] = $this->t('Timezone (Date)');
  106. }
  107. // Possible field added by Event contributed module.
  108. // @see https://api.drupal.org/api/drupal/modules%21user%21user.install/function/user_update_7002/7
  109. if ($this->getDatabase()->schema()->fieldExists('users', 'timezone_id')) {
  110. $fields['timezone_id'] = $this->t('Timezone (Event)');
  111. }
  112. return $fields;
  113. }
  114. }