1

I have a User migration in Drupal 8 which is coming from custom SQL tables. The user entity in Drupal 8 has a bunch of custom fields on it (First Name, Last Name, About, etc).

When I run the migration I can go into the field data tables in Drupal 8 and see that all of the values are there in the table where they should be, meaning they migrated into the new database correctly.

If I go to the users' edit pages (/user/#/edit), the fields are completely empty. If I then click save and wait for the page to reload, all of the migrated values magically appear in the form.

I tried running a post-migration script to manually save the users, but it still does not make the fields show up and I have to manually save the empty form for the values to show.

Here is the post-migration script I tried:

$ignored_users = [0, 1];
$uids = db_select('users', 'u')
  ->condition('uid', $ignored_users, 'NOT IN')
  ->fields('u', ['uid'])
  ->execute()
  ->fetchCol();

$users = user_load_multiple($uids);

foreach ($users as $user) {
  $user->save();
}

This is my config for the migration:

id: my_users
label: User Migration
migration_group: users
source:
  plugin: migrate_users
destination:
  plugin: entity:user
process:
  uid: uid
  name: name
  pass: pass
  mail: mail
  status: status
  created: created
  changed: changed
  access: access
  login: login
  langcode: language
  preferred_langcode: language
  preferred_admin_langcode: language
  init: init
  roles: roles
  user_picture:
    plugin: migration
    migration: my_files
    source: field_image_fid
  'field_about/value': body_value
  'field_about/summary': body_summary
  'field_about/format':
    plugin: default_value
    default_value: full_html
  field_first_name: field_first_name_value
  field_last_name: field_last_name_value
migration_dependencies:
  required:
    - my_files
dependencies:
  enforced:
    module:
      - my_migrate

Here is my user migration plugin:

class Users extends Nodes {

  /**
   * {@inheritdoc}
   */
  public function query() {
    return $this->select('users', 'u')
      ->fields('u', array_keys($this->baseFields()))
      ->condition('uid', 0, '>');
  }

  /**
   * {@inheritdoc}
   */
  public function fields() {
    $fields = $this->baseFields();
    $fields['field_about/format'] = $this->t('Format of body');
    $fields['field_about/value'] = $this->t('Full text of body');
    $fields['field_about/summary'] = $this->t('Summary of body');
    $fields['field_first_name'] = $this->t('First Name textfield');
    $fields['field_last_name'] = $this->t('Last Name textfield');

    return $fields;
  }

  /**
   * {@inheritdoc}
   */
  public function getIds() {
    return [
      'uid' => [
        'type' => 'integer',
        'alias' => 'u',
      ],
    ];
  }

  /**
   * Returns the user base fields to be migrated.
   *
   * @return array
   *   Associative array having field name as key and description as value.
   */
  protected function baseFields() {
    return [
      'uid' => $this->t('User ID'),
      'name' => $this->t('Username'),
      'pass' => $this->t('Password'),
      'mail' => $this->t('Email address'),
      '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'),
    ];
  }

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

    $row->setSourceProperty(
      'body_value',
      $this->getFieldValue($profile_nid, 'field_data_body', 'body_value')
    );
    $row->setSourceProperty(
      'body_summary',
      $this->getFieldValue($profile_nid, 'field_data_body', 'body_summary')
    );

    $row->setSourceProperty(
      'field_first_name_value',
      $this->getFieldValue($profile_nid, 'field_data_field_first_name', 'field_first_name_value')
    );

    $row->setSourceProperty(
      'field_last_name_value',
      $this->getFieldValue($profile_nid, 'field_data_field_last_name', 'field_last_name_value')
    );

    return parent::prepareRow($row);
  }

  /**
   * Gets the profile data node in Drupal 7 associated with a User account.
   *
   * @param int $uid
   *   Drupal user Id.
   *
   * @return int
   *   Team Member node Id.
   */
  private function getProfileData($uid) {
    return $this->select('field_data_field_drupal_user', 'fdp')
      ->fields('fdp', ['entity_id'])
      ->condition('field_drupal_user_target_id', $uid)
      ->execute()
      ->fetchField();
  }

}
0

The problem was the 'langocde' map in the config. The Drupal 7 langcode did not match the Drupal 8 langcode format.

When I set a default value for langcode in the migration config the fields showed as normal.

  langcode:
    plugin: default_value
    default_value: en

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.