Skip hooks during a Drupal 8 & 9 migration

15 Jan 2020

When migrating content with the Drupal 8 migrate module, the creation and updating of new entities may fire lots of custom module hooks. This may or may not be desired; if you have found yourself here, it probably interferes with the source data in a problematic way, or unnecessarily slows down the migration process.

The cleanest way I found to stop specific hooks for specific migrations, is to add a dummy/meta field to the migration and check for its value in the hook.

Include a dummy field in the migration

In the process section of the migration, add a field with a name that will not interfere with any field name of the target entity:

  1. # This is the field that will provide a custom hook
  2. # with the information about the migration.
  3. _migration:
  4. - plugin: default_value
  5. default_value: 'blog_categories'

This is an example migration with CSV as source and taxonomy terms as target:

  1. id: blog_categories
  2. label: 'Blog category migration'
  3. migration_group: default
  4.  
  5. source:
  6. plugin: csv
  7. path: 'public://migrations/blog_categories.csv'
  8. delimiter: ','
  9. enclosure: '"'
  10. header_row_count: 1
  11. ids: [tid]
  12.  
  13. process:
  14. name: name
  15.  
  16. # This is the field that will provide a custom hook
  17. # with the information about the migration.
  18. _migration:
  19. - plugin: default_value
  20. default_value: 'blog_categories'
  21.  
  22. destination:
  23. plugin: entity:taxonomy_term
  24. default_bundle: blog_categories
  25.  
  26. migration_dependencies:
  27. required: {}
  28. optional: {}

Check for the value in an entity hook

  1. /**
  2.  * Implements hook_entity_update().
  3.  */
  4. function my_module_entity_update(Drupal\Core\Entity\EntityInterface $entity) {
  5. if (isset($entity->_migration) && $entity->_migration === 'blog_categories') {
  6. return;
  7. }
  8.  
  9. // Some undesired custom hook logic.
  10. }

In this case the hook will never fire for this specific migration, but may fire for other migrations. Skipping the second condition will make sure the hook will never fire for migrations where the _migration dummy field is defined.

Comments

If you have a site that has real people editing content at the same time a migration is running, this might lead to an issue. Where the real user's edits are ignored because the migration is in progress.

Add new comment

The content of this field is kept private and will not be shown publicly.

Restricted HTML

  • Allowed HTML tags: <a href hreflang target> <em> <strong> <cite> <blockquote cite> <pre> <ul type> <ol start type> <li> <dl> <dt> <dd> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.

Get a quote in 24 hours

Wether a huge commerce system, or a small business website, we will quote the project within 24h of you pressing the following button: Get quote