Skip hooks during a Drupal 8 & 9 migration

15 Jan 2020



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
  5. source:
  6. plugin: csv
  7. path: 'public://migrations/blog_categories.csv'
  8. delimiter: ','
  9. enclosure: '"'
  10. header_row_count: 1
  11. ids: [tid]
  13. process:
  14. name: name
  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'
  22. destination:
  23. plugin: entity:taxonomy_term
  24. default_bundle: blog_categories
  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. }
  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.


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


Restricted HTML

  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.

Get a quote in 24 hours
