diff --git a/core/modules/migrate/src/Plugin/migrate/destination/EntityComment.php b/core/modules/migrate/src/Plugin/migrate/destination/EntityComment.php index 2deec37..d28e3fe 100644 --- a/core/modules/migrate/src/Plugin/migrate/destination/EntityComment.php +++ b/core/modules/migrate/src/Plugin/migrate/destination/EntityComment.php @@ -38,11 +38,11 @@ class EntityComment extends EntityContentBase { protected $entityQuery; /** - * The entity ID used for 'commented entity' objects in stub rows. + * An array of entity Ids for the 'commented entity' keyed by entity type. * - * @var string + * @var array */ - protected $stubCommentedEntityId; + protected $stubCommentedEntityIds; /** * Builds an comment entity destination. @@ -109,35 +109,46 @@ public function import(Row $row, array $old_destination_id_values = array()) { */ protected function getEntity(Row $row, array $old_destination_id_values) { if ($row->stub()) { - $stub_commented_entity_type = $row->getDestinationProperty('entity_type'); - - // While parent::getEntity() fills the bundle property for stub entities - // if it's still empty, here we must also make sure entity_id/entity_type - // are filled (so $comment->getCommentedEntity() always returns a value). - if (empty($this->stubCommentedEntityId)) { - - // Fill stub entity id. Any id will do, as long as it exists. - $entity_type = $this->entityManager->getDefinition($stub_commented_entity_type); - if ($id_key = $entity_type->getKey('id')) { - - $result = $this->entityQuery - ->getAggregate($stub_commented_entity_type) - ->aggregate($id_key, 'MIN') - ->execute(); - if ($result) { - $this->stubCommentedEntityId = $result[0][$id_key . '_min']; - } + $this->processStubRow($row); + } + + return parent::getEntity($row, $old_destination_id_values); + } + + /** + * Specific processing for stub rows. + * + * @param \Drupal\migrate\Row $row + * The row object. + * + * @throws \Drupal\migrate\MigrateSkipRowException + */ + protected function processStubRow(Row $row) { + $stub_commented_entity_type = $row->getDestinationProperty('entity_type'); + + // While parent::getEntity() fills the bundle property for stub entities + // if it's still empty, here we must also make sure entity_id/entity_type + // are filled (so $comment->getCommentedEntity() always returns a value). + if (empty($this->stubCommentedEntityIds[$stub_commented_entity_type])) { + + // Fill stub entity id. Any id will do, as long as it exists. + $entity_type = $this->entityManager->getDefinition($stub_commented_entity_type); + if ($id_key = $entity_type->getKey('id')) { + $result = $this->entityQuery + ->get($stub_commented_entity_type) + ->range(0, 1) + ->execute(); + if ($result) { + $this->stubCommentedEntityIds[$stub_commented_entity_type] = array_pop($result); } } - if (empty($this->stubCommentedEntityId)) { - throw new MigrateSkipRowException(t('Could not derive commented entity for comment %id', array('%id' => implode(':', $row->getSourceIdValues())))); - } - - $row->setDestinationProperty('entity_id', $this->stubCommentedEntityId); - $row->setDestinationProperty('entity_type', $stub_commented_entity_type); + } + if (empty($this->stubCommentedEntityIds[$stub_commented_entity_type])) { + throw new MigrateSkipRowException(t('Could not derive commented entity for comment %id', array('%id' => implode(':', $row->getSourceIdValues())))); } - return parent::getEntity($row, $old_destination_id_values); + $row->setDestinationProperty('entity_id', $this->stubCommentedEntityIds[$stub_commented_entity_type]); + $row->setDestinationProperty('entity_type', $stub_commented_entity_type); } }