Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
name = "Hedley migration"
description = "Populate content."
package = "Hedley"
core = 7.x
dependencies[] = devel_generate
dependencies[] = migrate
dependencies[] = migrate_extras
; Handlers
files[] = handlers/HedleyMigrateBase.php
files[] = handlers/HedleyMigrateNodes.php
;files[] = handlers/node/HedleyMigrateItems.php
;files[] = handlers/node/HedleyMigrateSales.php
files[] = handlers/user/HedleyMigrateUsers.php
files[] = handlers/node/HedleyMigrateSite.php
<?php
/**
* @file
* Contains \HedleyMigrateUsers.
*/
/**
* Class HedleyMigrateUsers.
*/
class HedleyMigrateUsers extends HedleyMigrateBase {
public $entityType = 'user';
/**
* HedleyMigrateUsers constructor.
*
* {@inheritdoc}
*/
public function __construct($arguments) {
parent::__construct($arguments);
$this->description = t('Import users from the CSV.');
$this->dependencies[] = 'HedleyMigrateSites';
$columns = [
['uuid', t('UUID')],
['name', t('Username')],
['pass', t('User password')],
['email', t('User email')],
['role', t('User role')],
['site', t('Site')],
['first_name', t('First name')],
['last_name', t('Last name')],
['phone', t('Phone')],
['status', t('Status')],
];
$source_file = $this->getMigrateDirectory() . '/csv/user.csv';
$options = ['header_rows' => 1];
$this->source = new MigrateSourceCSV($source_file, $columns, $options);
$this->destination = new MigrateDestinationUser();
$this->addFieldMapping('field_uuid', 'uuid');
$this->addFieldMapping('name', 'name');
$this->addFieldMapping('mail', 'email');
$this->addFieldMapping('pass', 'pass');
$this->addFieldMapping('role_names', 'role');
$this->addFieldMapping('field_first_name', 'first_name');
$this->addFieldMapping('field_last_name', 'last_name');
$this->addFieldMapping('field_phone', 'phone');
// Set the BO user status 'active' / 'pending'.
$this->addFieldMapping('field_user_status', 'status');
// Set the status property to active for all users. (Also pending users
// should be allowed to log in).
$this
->addFieldMapping('status')
->defaultValue(1);
$this->map = new MigrateSQLMap($this->machineName,
[
'name' => [
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
],
],
MigrateDestinationUser::getKeySchema()
);
}
/**
* Set user's site and dummy address.
*/
public function prepare($account, $row) {
$wrapper = entity_metadata_wrapper('user', $account);
// Set user's site.
if ($row->site) {
$node = node_load(hedley_site_get_nid_by_login_name($row->site));
if (!$node || $node->type != 'site') {
throw new Exception(format_string('User migration is trying to assign @site_name site.', ['@site_name' => $row->site]));
}
$wrapper->field_user_site->set($node);
}
// Set a dummy address, unless hedley_set_dummy_address_on_users_migration
// is set to FALSE.
if (variable_get('hedley_set_dummy_address_on_users_migration', TRUE)) {
hedley_user_set_random_address($wrapper);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.