3

I have migrate classes to import useres, taxonomy, content, etc. Some of these are being imported from a CSV source and need to be organized a little better either before or after an import.

How can I run custom functions before and after an import with the Migrate module?

5

For what you're asking (running a function prior to or after a single Migration) see the documentation for Less Commonly Implemented Migration Methods. They include:

  • function preImport()
  • function postImport()
  • function preRollback()
  • function postRollback()

Note this caveat from the documentation:

NOTE: If you are running your migrations through the web UI, which chunks the work into batches, if a given migration takes long enough to be broken into separate batches, these functions will be called for each batch. They are called each time the migration is invoked, not just the first (or last, in the complete case).

If you don't like that behavior, then in your preImport() or postImport() simply use a static variable to see if your custom logic has already run once. Lullabot Posted a good blog entry on using drupal_static back in 2011 on this subject.

With regards to per-record migration completion (like moving 1 song from legacy data store to drupal -- 1 "row" of a migration) take a look at the documentation for Commonly Implemented Migration Methods. They include:

  • function prepareRow($row)
  • function prepare($entity, stdClass $row)
  • function complete($entity, stdClass $row)
  • function createStub()
0

I found this especially useful for turning ON and OFF patterns for Path Auto settings with a simple variable_set('pathauto_node_MYTYPE_pattern') when migrating existing aliases into a new site.

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.