Last active
January 17, 2023 16:25
-
-
Save bmsimo/3a48f7cb266915060d3f05ef6a10d04b to your computer and use it in GitHub Desktop.
Drupal Users Migration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Drupal\MODULE_NAME\Plugin\migrate\source; | |
use Drupal\migrate\Annotation\MigrateSource; | |
use Drupal\migrate\Plugin\migrate\source\SqlBase; | |
use Drupal\migrate\Row; | |
/** | |
* Minimalistic example for a SqlBase source plugin. | |
* | |
* @MigrateSource( | |
* id = "users", | |
* source_module = "MODULE_NAME" | |
* ) | |
*/ | |
class Users extends SqlBase | |
{ | |
/** | |
* {@inheritdoc} | |
*/ | |
public function query() | |
{ | |
$query = $this->select('users_field_data', 'u') | |
->fields('u', [ | |
'uid', | |
'name', | |
'mail', | |
'pass', | |
'status', | |
'created', | |
'changed', | |
'access', | |
'login', | |
'init', | |
'timezone', | |
]); | |
// Skip the user with the ID 0. | |
$query->condition('u.uid', 0, '!='); | |
return $query; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function fields() | |
{ | |
$fields = [ | |
'uid' => $this->t('User ID'), | |
'name' => $this->t('Username'), | |
'mail' => $this->t('Email'), | |
'pass' => $this->t('Password'), | |
'status' => $this->t('Status'), | |
'created' => $this->t('Created'), | |
'access' => $this->t('Access'), | |
'login' => $this->t('Login'), | |
'init' => $this->t('Init'), | |
'timezone' => $this->t('Timezone'), | |
]; | |
return $fields; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getIds() | |
{ | |
return [ | |
'name' => [ | |
'type' => 'string', | |
'alias' => 'u', | |
], | |
]; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function prepareRow(Row $row) | |
{ | |
// This example shows how source properties can be added in | |
// prepareRow(). The source dates are stored as 2017-12-17 | |
// and times as 16:00. Drupal 8 saves date and time fields | |
// in ISO8601 format 2017-01-15T16:00:00 on UTC. | |
// We concatenate source date and time and add the seconds. | |
// The same result could also be achieved using the 'concat' | |
// and 'format_date' process plugins in the migration | |
// definition. | |
$row->setSourceProperty('created', $row->getSourceProperty('created') . 'T00:00:00'); | |
$row->setSourceProperty('access', $row->getSourceProperty('access') . 'T00:00:00'); | |
$row->setSourceProperty('login', $row->getSourceProperty('login') . 'T00:00:00'); | |
$row->setSourceProperty('changed', $row->getSourceProperty('changed') . 'T00:00:00'); | |
// Retrieve the roles for the user. | |
$uid = $row->getSourceProperty('uid'); | |
$roles = $this->getRoles($uid); | |
// Set the roles as a source property. | |
$row->setSourceProperty('roles', $roles); | |
return parent::prepareRow($row); | |
} | |
/** | |
* Returns the roles for the user. | |
* | |
* @param int $uid | |
* The user ID. | |
* | |
* @return array | |
* An array of roles. | |
*/ | |
protected function getRoles($uid) | |
{ | |
$roles = []; | |
$query = $this->select('user__roles', 'r') | |
->fields('r', ['roles_target_id']) | |
->condition('r.entity_id', $uid); | |
$result = $query->execute(); | |
foreach ($result as $record) { | |
$roles[] = $record['roles_target_id']; | |
} | |
return $roles; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment