I need to import a set of authors as part of a content entity. It is an array in a JSON object of id/name. I have a field in Drupal on the content type to be a user reference that accepts unlimited cardinality.

First of all, I don't know these users ahead of time, no way to query it, so I cannot just create a migration dependency and do that first. This seems to be a popular answer, but doesn't fit any of my use cases.

Second, I think you should be able to take the source data and loop each one, and create a user stub if they do not exist already. How do you do that? If the user does not exist yet they should be created, if they do exist, just look up the uid and put it into the target id per row.

This won't be the only spot I run into this, I will have content related to content that I will need to stub too. I won't know the latter content until I run their migrations later.

Is this even possible in D8?


Yes. This use scenario is covered by migration_lookup plugin with its stub_id property and making sure it's referenced migration (in your case, a user migration sourced from the JSON array) has defined all the required fields via the default_value plugin such that migration_lookup can rely on it to stub out a user before the stub_id migration can be run on its own to fill in fields for each referenced entity.

| improve this answer | |
  • Is there an example of this? – Kevin Mar 8 '19 at 23:43
  • I got it! That took longer than it should have but migration_lookup stubbed the user since it does not exist, then my other migration filled in the user objects. – Kevin Mar 11 '19 at 19:23
  • Yeah, I didn't have examples other than the linked docs. If you want to bolster your question with an example migration(s), I can bolster the answer using your question example code as an example. – Shawn Conn Mar 11 '19 at 22:39

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.