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