Migrating Users & roles from Drupal 7 to Drupal 10

29 / Jul / 2023 by deepak.tomar 0 comments

This blog will target the migration of users & roles from the Drupal 7 website to the Drupal 10 website.

Why should you migrate from Drupal 7 to Drupal 10?

November 2023 will be the end of life for Drupal 7. With the release of Drupal 10, it is essential to migrate the website from outdated versions for better community support, technological advancement, and security.

We will start with creating a role migration definition.

First step: Create migration definition

Add the YAML file in the location /custom_module/config/install/migrate_plus.migration.d7_user_role.yml:

id: d7_user_role
label: User roles
migration_tags:
- Drupal 7
- Configuration
source:
plugin: d7_user_role
process:
id:
-
plugin: machine_name
source: name
-
plugin: user_update_8002
label: name
permissions:
-
plugin: static_map
source: permissions
bypass: true
- plugin: flatten
weight: weight
destination:
plugin: entity:user_role
migration_dependencies:
optional:
- block_content_type
- contact_category
- d7_comment_type
- d7_filter_format
- d7_node_type
- d7_shortcut_set
- d7_taxonomy_vocabulary
- d7_taxonomy_vocabulary_translation

AND
Add YAML file in the location /custom_module/config/install/migrate_plus.migration.d7_user.yml:

id: d7_user
label: User accounts
audit: true
migration_tags:
- Drupal 7
- Content
class: Drupal\user\Plugin\migrate\User
source:
plugin: d7_user
process:
name: name
pass: pass
mail: mail
created: created
access: access
login: login
status: status
timezone: timezone
langcode:
plugin: user_langcode
source: entity_language
fallback_to_site_default: false
preferred_langcode:
plugin: user_langcode
source: language
fallback_to_site_default: true
preferred_admin_langcode:
plugin: user_langcode
source: language
fallback_to_site_default: true
init: init
# Custom Fields.
field_first_name: d7_first_name
field_last_name: d7_last_name
roles:
plugin: migration_lookup
migration: d7_user_role
source: roles
user_picture:
-
plugin: default_value
source: picture
default_value: null
-
plugin: migration_lookup
migration: d7_file
destination:
plugin: entity:user
migration_dependencies:
required:
- d7_user_role
optional:
- d7_field_instance
- language
- default_language

For user image migration, a separate YAML file must be added by ID name d7_file.

Second step: Add a definition for source plugin

Create the php class in location /custom_module/src/Plugin/migrate/source/User.php

<?php
namespace Drupal\d10_users_migration\Plugin\migrate\source;
use Drupal\migrate\Row;
use Drupal\migrate\Plugin\migrate\source\SqlBase;
/**
* Extract users from Drupal 7 database.
*
* @MigrateSource(
* id = "d7_user",
* source_module = "migrate_plus"
* )
*/
class User extends SqlBase {
/**
* {@inheritdoc}
*/
public function query() {
return $this->select('users', 'u')
->fields('u', array_keys($this->baseFields()))
->condition('uid', 0, '>')->orderBy('uid', 'ASC');
}
/**
* {@inheritdoc}
*/
public function fields() {
$fields = $this->baseFields();
$fields['first_name'] = $this->t('First Name');
$fields['last_name'] = $this->t('Last Name');
return $fields;
}
/**
* {@inheritdoc}
*/
public function prepareRow(Row $row) {
$uid = $row->getSourceProperty('uid');
// Get first name from d7 db.
$result = $this->getDatabase()->query('
SELECT
fld.field_first_name_value
FROM
{field_data_field_first_name} fld
WHERE
fld.entity_id = :uid
', [':uid' => $uid]);
foreach ($result as $record) {
$row->setSourceProperty('d7_first_name', $record->field_first_name_value);
}
// Get last name value from d7 db.
$result = $this->getDatabase()->query('
SELECT
fld.field_last_name_value
FROM
{field_data_field_last_name} fld
WHERE
fld.entity_id = :uid
', [':uid' => $uid]);
foreach ($result as $record) {
$row->setSourceProperty('d7_last_name', $record->field_last_name_value);
}
return parent::prepareRow($row);
}
/**
* {@inheritdoc}
*/
public function getIds() {
return [
'uid' => [
'type' => 'integer',
'alias' => 'u',
],
];
}
/**
* User base fields.
*
* @return array
* Base fields array.
*/
protected function baseFields() {
$fields = [
'uid' => $this->t('User ID'),
'name' => $this->t('Username'),
'pass' => $this->t('Password'),
'mail' => $this->t('Email address'),
'signature' => $this->t('Signature'),
'signature_format' => $this->t('Signature format'),
'created' => $this->t('Registered timestamp'),
'access' => $this->t('Last access timestamp'),
'login' => $this->t('Last login timestamp'),
'status' => $this->t('Status'),
'language' => $this->t('Language'),
'picture' => $this->t('Picture'),
'init' => $this->t('Init'),
];
return $fields;
}
/**
* {@inheritdoc}
*/
public function bundleMigrationRequired() {
return FALSE;
}
/**
* {@inheritdoc}
*/
public function entityTypeId() {
return 'user';
}
}

Used methods —

To fetch data from the Drupal 7 database, the query() function is used.

prepareRow() is called to load data from tables $row->setSourceProperty() will provide any property in our “process” step.

baseFields() describes the basic user fields the query() function uses.

Drush commands to start migration:
1. For role migration
drush migrate-import d7_user_role

2. For users Migration
drush migrate-import d7_user

This process will migrate the users along with the password, custom & base fields on our Drupal 10 website.

FOUND THIS USEFUL? SHARE IT

Tag -

migration

Leave a Reply

Your email address will not be published. Required fields are marked *