1

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

4
  • 1
    Welcome to Drupal Answers :) Where’s it breaking down exactly? Have you verified that you’re getting the expected results from your entity query, for example? Have you confirmed that the 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
    – Clive
    Oct 27, 2018 at 13:43
  • The migration annotation is missing. Something like /* @MigrateProcessPlugin( id = "user_lookup") */ on top of your class file.
    – Hudri
    Oct 28, 2018 at 7:46
  • @Hudri - I updated the post to include the whole file UserLookup.php. Let me know if that is what you were referring to. Thanks Oct 28, 2018 at 22:29
  • @Clive - Thanks for commenting. I have verified that the value is getting past to the UserLookup class and the lookup is happening correctly. It is the return $user_uid; that is not happening. Actually, the return does not seem to work at all. I have tried returning other hard-coded numbers and it never goes back to migration. Oct 28, 2018 at 22:29

1 Answer 1

0

I think the problem might be the source on the skip_on_empty plugin. It looks like you're trying to refer to the result of the previous plugin to obtain the destination uid; I don't think you can do that until after all plugins have run on the field.

Even if you can make that reference, there's a special syntax for referring to destination fields on the source side, which is an @ symbol plus the field name (e.g. '@uid').

A solution that should remove all doubt would be to load the uid in a fake field, and re-use it for the proper destination field. For example:

process:
  _uid:
    plugin: user_lookup
    source: EMPNO
    value_key: field_employee_id
    entity_type: user
    bundle: 1
    bundle_key: status
    ignore_case: true
  uid:
    plugin: skip_on_empty
    method: row
    source: '@_uid'
    message: 'no empno found'

Of course that might be wrong, and you may just have meant to use EMPNO instead of uid for the source, in which case you should just fix that and move the skip_on_empty plugin so it runs first, saving yourself DB queries with empty conditions.

3
  • I see what you are saying and I made the appropriate changes. Your assumption is correct. The import file has an employee ID and I am using that to get the actual uid of the user. Then trying to pass that back to the migration to match up the user. So I made the changes you suggested and the below is the result of the migration. Oct 29, 2018 at 18:37
  • I had to double check somethings and also uninstall / reinstall the module and the import is now working. Thanks very much for your assistance. Oct 30, 2018 at 15:56
  • Great news, no problem!
    – Clive
    Oct 30, 2018 at 16:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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