2

Following [this][1], I have done migration for users from Drupal 7 to Drupal 8, but I got stuck with assigning roles to the specified users.

In user.php file, my code for mapping roles to assigned users is the following.

public function prepareRow(Row $row) {
    $uid = $row->getSourceProperty('uid');
    // user_role

    $result = $this->getDatabase()->query('
      SELECT
        r.rid, r.name, ur.uid
      FROM
        {role} r, {users_roles} ur
      WHERE
        r.rid = ur.rid
      AND
        ur.uid = :uid
    ', array(':uid' => $uid));
    foreach ($result as $record) {
      $row->setSourceProperty('roles', $record->name );
    }

    // local_market field of type entity_reference
    $result = $this->getDatabase()->query('
      SELECT
        fld.field_local_market_target_id
      FROM
        {field_data_field_local_market} fld
      WHERE
        fld.entity_id = :uid
    ', array(':uid' => $uid));
    foreach ($result as $record) {
      if (!is_null($record->tids)) {
        $row->setSourceProperty('local_market', explode(',', $record->tids) );
      }
    }



    return parent::prepareRow($row);
  }

Except roles and custom field, field_local_market, all users migrated from Drupal 7 to Drupal 8.

destination:
  plugin: entity:user

process:
  uid: uid
  name: name
  pass: pass
  mail: mail
  status: status
  created: created
  changed: changed
  access: access
  login: login
  timezone: timezone
  langcode: language
  preferred_langcode: language
  preferred_admin_langcode: language
  init: init
  field_local_market: local_market 
  field_user_role: user_role

What's the right way to assign roles to the users?

Migration of roles is done from Drupal 7 to Drupal 8 by using drush migrate_upgrade; the only task left is assigning/mapping these roles to certain UIDs or users.

2

You can migrate users with roles without migrating roles using migrate static_map

In migrate set

  process:
  uid: null
  name: name
  pass: pass
  mail: mail
  status: status
  created: created
  access: access
  login: login
  roles:
    plugin: static_map
    bypass: true
    source: roles
    map:
      5: new_role_name
      3: editor

and in the source plugin you have to set the roles in prepareRow

  /**
   * {@inheritdoc}
   */
  public function prepareRow(Row $row) {
    $uid = $row->getSourceProperty('uid');

    $query = $this->select('users_roles', 'r');
    $query->fields('r', ['rid']);
    $query->condition('r.uid', $uid, '=');
    $record = $query->execute()->fetchAllKeyed();
    $row->setSourceProperty('roles', array_keys($record));

    return parent::prepareRow($row);
  }
0

Few things you can try:

1) You need to pass the Role ID instead of the Role name.

2) Ensure you're passing an array and you actually have results in the query result:

$roleIds = array_map(function ($record){
  return $record->rid;
}, $result);

// Make sure $roleIds is an array.    
$row->setSourceProperty('roles', $roleIds);

3) As your roles are migrated in another migration, the mapping should be done by using a special plugin "migration". E.g:

roles:
  plugin: migration
  source: roles
  migration: the_migration_name_which_migrated_user_roles

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.