0

I have a single migration out of a larger group, that needs to be run as the administrator or an entity reference fails to map the User entity properly. I'd love ideas on what I might change so that I wouldn't have to be an administrator to entity reference users, but failing that:

How can I check what user the migration is currently running as? Basically I want to be sure that drush has been invoked with --user=1 or throw an exception with a reminder that this migration needs to be run as administrator.

3
  • 1
    Not familiar with the migrate module - does it run inside a standard Drupal bootstrap? If so $GLOBALS['user']->uid should be avilable
    – Clive
    Nov 12, 2015 at 15:17
  • That worked for me, if you care to make it an answer and if there is no more specific answer about how to entity reference users in the migrate module within a reasonable period of time I'll accept it.
    – UltraBob
    Nov 13, 2015 at 2:10
  • Even better, for someone having the same issue I was having, I was able to put this in the preImport method: $GLOBALS['user']->previousid = $GLOBALS['user']->uid; $GLOBALS['user']->uid = 1; and this in postImport: $GLOBALS['user']->uid = $GLOBALS['user']->previousid;
    – UltraBob
    Nov 13, 2015 at 9:20

1 Answer 1

0

In the interest of trying to up the percentage of Drupal Questions that have an answer, I'll post the solution that I came to as a result of Clive's comment.

I added a preImport and postImport method to my migration class as follows:

  public function preImport() {
    parent::preImport();
    $GLOBALS['user']->previousid = $GLOBALS['user']->uid;
    $GLOBALS['user']->uid = 1;
    if ($GLOBALS['user']->uid != 1) {
      throw new MigrateException('Run Staff Contributor Migration as user 1.');
    }
  }

  public function postImport() {
    parent::postImport();
    $GLOBALS['user']->uid = $GLOBALS['user']->previousid;
  }

This ensures that this particular migration is run as user 1 and returns the user value back to whatever it had been at the end of the migration. It also throws an exception if for some reason the user switch failed.

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.