Migrate users and roles from Drupal 7 to Drupal 8

Development |

In this article, we will be building a custom migration which will import users from a Drupal 7 site into a Drupal 8 site. The migration will include the standard user profile fields like username and email address, plus a few custom fields added to the user profile.

You'll need - clean install of Drupal 7, with the following customizations:

    In Admin -> Configuration -> People -> Account Settings -> Manage Fields, add the following fields:

  • Street (textfield)

  • Phone (textfield)

Once the field configuration is complete, you'll need to create a few users so you have data to migrate.

In your Drupal 8 website, make the same fields as on your Drupal 7 website.

First thing you'll to do is to write a role migration.

Create migrate_plus.migration.d7_user_role.yml


            id: d7_user_role
            label: User roles
            migration_group: d7
            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
                 map:
                   'use PHP for block visibility': 'use PHP for settings'
                   'administer site-wide contact form': 'administer contact forms'
                   'post comments without approval': 'skip comment approval'
                   'edit own blog entries': 'edit own blog content'
                   'edit any blog entry': 'edit any blog content'
                   'delete own blog entries': 'delete own blog content'
                   'delete any blog entry': 'delete any blog content'
                   'create forum topics': 'create forum content'
                   'delete any forum topic': 'delete any forum content'
                   'delete own forum topics': 'delete own forum content'
                   'edit any forum topic': 'edit any forum content'
                   'edit own forum topics': 'edit own forum content'
               - plugin: flatten
             weight: weight
            destination:
             plugin: entity:user_role
            migration_dependencies:
             optional:
               - d7_filter_format

            

Use the same migration_group like in the previous examples.

Now we are ready to write a user migration.

Create migrate_plus.migration.d7_user.yml file.


            id: d7_user
            label: User accounts
            migration_group: d7
            migration_tags:
             - Drupal 7
            class: Drupal\user\Plugin\migrate\User
            source:
             plugin: d7_user
            process:
             # If you are using this file to build a custom migration,
             # consider removing the uid field to allow
             # incremental migrations.
             uid: uid
             name: name
             pass: pass
             mail: mail
             created: created
             access: access
             login: login
             status: status
             timezone: timezone
             langcode:
               plugin: user_langcode
               source: 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
             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
               - d7_file
               - language
               - default_language
               - user_picture_field_instance
               - user_picture_entity_display
               - user_picture_entity_form_display

            

Type drush migrate-status

As we discussed previously, the first thing we have to do is a role migration.

drush migrate:import d7_user_role

Now we are ready for a user migration

drush migrate:import d7_user

That’s all for now.

RELATED ARTICLES