function User::prepareRow

Same name in this branch
  1. 11.x core/modules/user/src/Plugin/migrate/source/d6/User.php \Drupal\user\Plugin\migrate\source\d6\User::prepareRow()
Same name in other branches
  1. 9 core/modules/user/src/Plugin/migrate/source/d6/User.php \Drupal\user\Plugin\migrate\source\d6\User::prepareRow()
  2. 9 core/modules/user/src/Plugin/migrate/source/d7/User.php \Drupal\user\Plugin\migrate\source\d7\User::prepareRow()
  3. 8.9.x core/modules/user/src/Plugin/migrate/source/d6/User.php \Drupal\user\Plugin\migrate\source\d6\User::prepareRow()
  4. 8.9.x core/modules/user/src/Plugin/migrate/source/d7/User.php \Drupal\user\Plugin\migrate\source\d7\User::prepareRow()
  5. 10 core/modules/user/src/Plugin/migrate/source/d6/User.php \Drupal\user\Plugin\migrate\source\d6\User::prepareRow()
  6. 10 core/modules/user/src/Plugin/migrate/source/d7/User.php \Drupal\user\Plugin\migrate\source\d7\User::prepareRow()

Overrides SourcePluginBase::prepareRow

File

core/modules/user/src/Plugin/migrate/source/d7/User.php, line 69

Class

User
Drupal 7 user source from database.

Namespace

Drupal\user\Plugin\migrate\source\d7

Code

public function prepareRow(Row $row) {
    $uid = $row->getSourceProperty('uid');
    $roles = $this->select('users_roles', 'ur')
        ->fields('ur', [
        'rid',
    ])
        ->condition('ur.uid', $uid)
        ->execute()
        ->fetchCol();
    $row->setSourceProperty('roles', $roles);
    $row->setSourceProperty('data', unserialize($row->getSourceProperty('data') ?? ''));
    // If this entity was translated using Entity Translation, we need to get
    // its source language to get the field values in the right language.
    // The translations will be migrated by the d7_user_entity_translation
    // migration.
    $entity_translatable = $this->isEntityTranslatable('user');
    $source_language = $this->getEntityTranslationSourceLanguage('user', $uid);
    $language = $entity_translatable && $source_language ? $source_language : $row->getSourceProperty('language');
    $row->setSourceProperty('entity_language', $language);
    // Get Field API field values.
    foreach ($this->getFields('user') as $field_name => $field) {
        // Ensure we're using the right language if the entity and the field are
        // translatable.
        $field_language = $entity_translatable && $field['translatable'] ? $language : NULL;
        $row->setSourceProperty($field_name, $this->getFieldValues('user', $field_name, $uid, NULL, $field_language));
    }
    // Get profile field values. This code is lifted directly from the D6
    // ProfileFieldValues plugin.
    if ($this->getDatabase()
        ->schema()
        ->tableExists('profile_value')) {
        $query = $this->select('profile_value', 'pv')
            ->fields('pv', [
            'fid',
            'value',
        ]);
        $query->leftJoin('profile_field', 'pf', '[pf].[fid] = [pv].[fid]');
        $query->fields('pf', [
            'name',
            'type',
        ]);
        $query->condition('uid', $row->getSourceProperty('uid'));
        $results = $query->execute();
        foreach ($results as $profile_value) {
            if ($profile_value['type'] == 'date') {
                $date = unserialize($profile_value['value']);
                $date = date('Y-m-d', mktime(0, 0, 0, $date['month'], $date['day'], $date['year']));
                $row->setSourceProperty($profile_value['name'], [
                    'value' => $date,
                ]);
            }
            elseif ($profile_value['type'] == 'list') {
                // Explode by newline and comma.
                $row->setSourceProperty($profile_value['name'], preg_split("/[\r\n,]+/", $profile_value['value']));
            }
            else {
                $row->setSourceProperty($profile_value['name'], [
                    $profile_value['value'],
                ]);
            }
        }
    }
    return parent::prepareRow($row);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.