Summary
The Drupal 8 content entities are using typed data that can be validated (see \Drupal\Core\Entity\ContentEntityBase::validate()
).
The good news is that core's migrate
module now supports entity validation that may help to sift out undesired content from being imported. Let's take a look at the configuration:
id: my_migration
source:
# ...
process:
# ...
destination:
plugin: 'entity:node'
default_bundle: page
# Run entity and fields validation before saving an entity.
# @see \Drupal\Core\Entity\FieldableEntityInterface::validate()
validate: true
The only thing added is validate: true
. This simple addition can easily be brought to either new or existing migration yamls to avoid the aftermath of importing broken data. This is not turned on by default as it could lead to failures in migrations that were previously functional.
Detailed description
The \Drupal\Core\Entity\FieldableEntityInterface::isValidationRequired()
returns FALSE
by default for the most entity types. (see \Drupal\Core\Entity\ContentEntityBase::$validationRequired
) which makes valid the execution of the code like \Drupal::entityTypeManager()->getStorage('node')->create(['type' => 'page'])->save()
, despite the fact that title
is mandatory. That's also how the import to content entity behaves; by default migrate
module - literally creates an object from source and fires the save()
method, without calling validate()
. In some cases missing validation may result in broken entities that would be hard to find after a migration.
Comments
But how to run migration as admin now?
So validate function is now checking if entity has an owner and if so using account switcher to set owner as current user. But what if I want to run migration as admin? In my case I want to assign disabled user as entity owner. In that case validation fails. User must be enabled or have "administer users" permission. Admin could do that, but with behavior of assigning entity owner as current user it can be done. IMHO there should be the difference between user that is running the migration and users involved in migration and should be possible to choose with which account validation should be made.