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:
# This is the field that will provide a custom hook # with the information about the migration. _migration: - plugin: default_value default_value: 'blog_categories'
This is an example migration with CSV as source and taxonomy terms as target:
id: blog_categories label: 'Blog category migration' migration_group: default source: plugin: csv path: 'public://migrations/blog_categories.csv' delimiter: ',' enclosure: '"' header_row_count: 1 ids: [tid] process: name: name # This is the field that will provide a custom hook # with the information about the migration. _migration: - plugin: default_value default_value: 'blog_categories' destination: plugin: entity:taxonomy_term default_bundle: blog_categories migration_dependencies: required: {} optional: {}
Check for the value in an entity hook
/** * Implements hook_entity_update(). */ function my_module_entity_update(Drupal\Core\Entity\EntityInterface $entity) { return; } // Some undesired custom hook logic. }
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
Thanks for the tip! An alternative approach could be to use migrate_status.
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