function EntityContentBase::validateEntity

Same name in other branches
  1. 8.9.x core/modules/migrate/src/Plugin/migrate/destination/EntityContentBase.php \Drupal\migrate\Plugin\migrate\destination\EntityContentBase::validateEntity()
  2. 10 core/modules/migrate/src/Plugin/migrate/destination/EntityContentBase.php \Drupal\migrate\Plugin\migrate\destination\EntityContentBase::validateEntity()
  3. 11.x core/modules/migrate/src/Plugin/migrate/destination/EntityContentBase.php \Drupal\migrate\Plugin\migrate\destination\EntityContentBase::validateEntity()

Overrides MigrateValidatableEntityInterface::validateEntity

1 call to EntityContentBase::validateEntity()
EntityContentBase::import in core/modules/migrate/src/Plugin/migrate/destination/EntityContentBase.php

File

core/modules/migrate/src/Plugin/migrate/destination/EntityContentBase.php, line 199

Class

EntityContentBase
Provides destination class for all content entities lacking a specific class.

Namespace

Drupal\migrate\Plugin\migrate\destination

Code

public function validateEntity(FieldableEntityInterface $entity) {
    // Entity validation can require the user that owns the entity. Switch to
    // use that user during validation.
    // As an example:
    // @see \Drupal\Core\Entity\Plugin\Validation\Constraint\ValidReferenceConstraint
    $account = $entity instanceof EntityOwnerInterface ? $entity->getOwner() : NULL;
    // Validate account exists as the owner reference could be invalid for any
    // number of reasons.
    if ($account) {
        $this->accountSwitcher
            ->switchTo($account);
    }
    // This finally block ensures that the account is always switched back, even
    // if an exception was thrown. Any validation exceptions are intentionally
    // left unhandled. They should be caught and logged by a catch block
    // surrounding the row import and then added to the migration messages table
    // for the current row.
    try {
        $violations = $entity->validate();
    } finally {
        if ($account) {
            $this->accountSwitcher
                ->switchBack();
        }
    }
    if (count($violations) > 0) {
        throw new EntityValidationException($violations);
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.