2

I’m able to migrate users datas from a csv file with migrate, migrate_tools, migrate_source_csv and drush

Now, i’m trying to import datas from the same csv file to the Profile module (which provides configurable user profiles).

Here is my configuration file for user import. Obviously, i I add the profile fields in it (here field_profile_last_name, it doesn’t work).

id: user_migrate
migration_tags: null
migration_group: null
label: 'User migration from CSV'
source:
  plugin: csv
  path: /path-to-csv/users.csv
  header_row_count: 1
  keys:
    - weblogin
process:
  # user fields with custom fields : works !
  name: weblogin
  mail: email
  roles: roles
  field_last_name: nom_salarie
  field_first_name: prenom_salarie
  field_password_web_services: weblogin
  status: status
  field_profile_address: adresse1
  field_code: code
  field_date_de_naissance: date_naissance
  field_numero_societe: numsoc
  field_profile_function: qualification
  # profile fields : doesn't work
  field_profile_last_name: nom_salarie
destination:
  plugin: 'entity:user'
migration_dependencies:
  required: {  }
  optional: {  }

How can i migrate datas to the profile module ? What is the entity for this module.

0

I post an answer to my question. Feel free to comment or edit my answer in order to improve.

I’ve migrated users datas from a csv file. If you need help to do this, you can follow instructions in this questions : Import users into Drupal with data from a CSV file

Now, i import the profile data in a migration process that maintains the relationships between source and destination identifiers in map table.

So the profile is related to the correct uid.

More information on this here (Process plugin: migration_lookup):

https://www.drupal.org/docs/8/api/migrate-api/migrate-process-plugins/process-plugin-migration_lookup-formerly-migration

The configuration file looks like this. You have to adapt some values as :

uid: source: xxx

This takes the value of the xxx property in the source data, and looks it up in the map table associated with the users migration, returning the resulting user ID and assigning it to the destination uid property.

You have to adapt the fields of course and the key to your needs.

id: profile_migrate
migration_tags: null
migration_group: null
label: 'Profile migration from CSV'
source:
  plugin: csv
  path: /path/to/csv/users.csv
  header_row_count: 1
  keys:
    - weblogin
destination:
  plugin: entity:profile
process:
   type:
    plugin: default_value
    default_value: 'profile'
   uid:
     plugin: migration_lookup
     no_stub: true
     # previous user migration
     migration: user_migrate
     # property in the source data
     source: weblogin
   field_profile_last_name:
     plugin: get
     source: nom_salarie
   field_profile_first_name: prenom_salarie
   field_profile_address: adresse1
   field_date_de_naissance: date_naissance
   field_code: code
   field_numero_societe: numsoc
   field_profile_function: qualification
migration_dependencies:
  required: {  }
  optional: {  }
0

I have migrated profiles recently from D7 to D9. Here is what I did.

  1. Get csv data from drupal 7 with content you want
  2. Create a form to upload a csv file
  3. In submit function here is the code what I wrote
public function submitForm(array &$form, FormStateInterface $form_state) {
  
  // Fetch the array of the file stored temporarily in database.
  $csv_file = $form_state->getValue('csv_upload');

  // Load the object of the file by it's fid.
  $file = File::load( $csv_file[0] );

  // Set the status flag permanent of the file object.
  $file->setPermanent();

  // Save the file in database.
  $file->save();

  // You can use any sort of function to process your data. The goal is to 
  // get each 'row' of data into an array.
  // If you need to work on how data is extracted, process it here.
  $data = $this->csvtoarray($file->getFileUri(), ',');
  $connection = \Drupal::database();

  foreach($data as $key => $row) {
    $profile = Profile::create([
      'type' => 'main',
      'uid' => $row['uid'],    
      'field_blurb' => [
        'value'  =>  $row['Blurb'],
        'format' => 'full_html'
      ],
      'field_link' => [
        'uri'=> $row['link'], 
        'title' => $row['text']
      ],
      'field_reference' => [
        'target_id' =>  $row['tid'],
      ],
      'field_icon' => [
        "target_id"  =>  $row['fid'],
      ],
    ]);
    $profile->save();
  }

}

public function csvtoarray($filename='', $delimiter){
  if (!file_exists($filename) || !is_readable($filename)) {
    return FALSE;
  }
  $header = NULL;
  $data = [];

  if (($handle = fopen($filename, 'r')) !== FALSE ) {
    while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE) {
      if (!$header) {
        $header = $row;
      }
      else {
        $data[] = array_combine($header, $row);
      }
    }
    fclose($handle);
  }

  return $data;
}

` 4. This creates a profiles with id and connects them to each user with user id from csv file

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.