only in patch2: unchanged: --- a/core/modules/migrate/src/Plugin/migrate/destination/EntityContentBase.php +++ b/core/modules/migrate/src/Plugin/migrate/destination/EntityContentBase.php @@ -8,6 +8,7 @@ use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Entity\FieldableEntityInterface; use Drupal\Core\Field\FieldTypePluginManagerInterface; +use Drupal\Core\Session\AccountSwitcherInterface; use Drupal\Core\TypedData\TranslatableInterface; use Drupal\Core\TypedData\TypedDataInterface; use Drupal\migrate\Audit\HighestIdInterface; @@ -17,6 +18,7 @@ use Drupal\migrate\MigrateException; use Drupal\migrate\Plugin\MigrateIdMapInterface; use Drupal\migrate\Row; +use Drupal\user\EntityOwnerInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -102,6 +104,13 @@ class EntityContentBase extends Entity implements HighestIdInterface, MigrateVal */ protected $fieldTypeManager; + /** + * The account switcher service. + * + * @var \Drupal\Core\Session\AccountSwitcherInterface + */ + protected $accountSwitcher; + /** * Constructs a content entity. * @@ -133,7 +142,7 @@ public function __construct(array $configuration, $plugin_id, $plugin_definition */ public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) { $entity_type = static::getEntityTypeId($plugin_id); - return new static( + $instance = new static( $configuration, $plugin_id, $plugin_definition, @@ -143,6 +152,8 @@ public static function create(ContainerInterface $container, array $configuratio $container->get('entity_field.manager'), $container->get('plugin.manager.field.field_type') ); + $instance->accountSwitcher = $container->get('account_switcher'); + return $instance; } /** @@ -184,8 +195,18 @@ public function isEntityValidationRequired(FieldableEntityInterface $entity) { * {@inheritdoc} */ public function validateEntity(FieldableEntityInterface $entity) { + // Entity validation can require the user that owns the entity. Switch to + // use that user during validation. + if (($entity instanceof EntityOwnerInterface) && $user = $entity->getOwner()) { + $this->getAccountSwitcher()->switchTo($user); + } + $violations = $entity->validate(); + if (($entity instanceof EntityOwnerInterface) && $user = $entity->getOwner()) { + $this->getAccountSwitcher()->switchBack(); + } + if (count($violations) > 0) { throw new EntityValidationException($violations); } @@ -375,4 +396,17 @@ public function getHighestId() { return (int) current($values); } + /** + * Get the account switcher service. + * + * @return \Drupal\Core\Session\AccountSwitcherInterface + * The account switcher service. + */ + protected function getAccountSwitcher(): AccountSwitcherInterface { + if (!isset($this->accountSwitcher)) { + $this->accountSwitcher = \Drupal::service('account_switcher'); + } + return $this->accountSwitcher; + } + }