I have a migration and it is very simple. I have a CSV file of employee number and years of service. I have added the two fields to the users in the system and populate them when the users have been added. I now I have a yearly file to update the years of service value. I created the below Migration to import/update the seniority:
id: seniority_import
label: Seniority CSV file import
description: Seniority CSV file import
migration_group: timeoff
migration_tags:
- seniority_timeoff
langcode: en
status: true
dependencies:
enforced:
module:
- timeoff_seniority
source:
plugin: csv
track_changes: true
path: "/Users/rfulcher/Downloads/Seniority2017-2.csv"
keys:
- EMPNO
column_names:
0:
EMPNO: "Employee Number"
1:
SENIORITY: "Employee Seniority"
process:
uid:
-
plugin: user_lookup
source: EMPNO
value_key: field_employee_id
entity_type: user
bundle: 1
bundle_key: status
ignore_case: true
-
plugin: skip_on_empty
method: row
source: uid
message: 'no empno found'
field_employee_id: {}
field_seniority: SENIORITY
destination:
plugin: entity:user
overwrite_properties:
- field_seniority
I also create the custom process plugin to lookup the uid of the user using the employee number
<?php
namespace Drupal\timeoff_seniority\Plugin\migrate\process;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\Row;
/**
* Returns a user if exists.
*
* Example:
*
* @code
* process:
* field_your_field_name:
* -
* plugin: your_plugin_name
* source: some_source_value
* @endcode
*
*
* @see \Drupal\migrate\Plugin\MigrateProcessInterface
*
* @MigrateProcessPlugin(
* id = "user_lookup"
* )
*/
class UserLookup extends ProcessPluginBase {
/**
* {@inheritdoc}
*/
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
$user_uid = $this->getUserByEmpNo($value);
if ($user_uid == FALSE){
drush_print('usernotfound for ' . $value . ' returning 0');
return 0;
} else {
drush_print('userfound:'.$user_uid.': for ' . $value);
return $user_uid;
}
}
protected function getUserByEmpNo($employeenumber) {
$id = \Drupal::entityQuery('user')
->condition('field_employee_id', $employeenumber)
->execute();
$users = \Drupal\user\Entity\User::loadMultiple($id);
if (!empty($users)) {
foreach ($users as $user) {
$update_id = $user->id();
return $update_id;
}
} else {
return FALSE;
}
}
public function prepareRow($row) {
}
}
The import runs and I find the uid and return the value but it is like the migration ignores it and skips the row.
Am I missing something. I have worked on this way too long and this should be simple but I must not be understanding how this is supposed to work or something. I am totally at a loss.
Simple csv file with just two fields empno, seniority. Nothing fancy. Any help with this would be appreciated, thanks very much.
Thanks
value
is being passed through correctly? Without a bit more information as to where the problem lies, it’s going to be difficult to advise without reproducing your whole migration (which people might not be willing to do). If you could add some information to the post which details what your own debugging has for you so far, that would be very useful and probably get you a quicker answer/* @MigrateProcessPlugin( id = "user_lookup") */
on top of your class file.