Issue #2340401 by benjy, roderik, mikeryan, dawehner, andypost, chx, phenaproxima, larowlan: Fill commented entity during stub comment saving

8.0.x
webchick 2015-09-11 14:00:52 -07:00
parent 11cd1c5f03
commit 24e346cce9
3 changed files with 71 additions and 19 deletions

View File

@ -9,8 +9,10 @@ namespace Drupal\comment\Plugin\migrate\destination;
use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\Query\QueryFactory;
use Drupal\Core\State\StateInterface; use Drupal\Core\State\StateInterface;
use Drupal\migrate\Entity\MigrationInterface; use Drupal\migrate\Entity\MigrationInterface;
use Drupal\migrate\MigrateException;
use Drupal\migrate\Plugin\migrate\destination\EntityContentBase; use Drupal\migrate\Plugin\migrate\destination\EntityContentBase;
use Drupal\migrate\Row; use Drupal\migrate\Row;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
@ -29,6 +31,20 @@ class EntityComment extends EntityContentBase {
*/ */
protected $state; protected $state;
/**
* The entity query object.
*
* @var \Drupal\Core\Entity\Query\QueryInterface
*/
protected $entityQuery;
/**
* An array of entity IDs for the 'commented entity' keyed by entity type.
*
* @var array
*/
protected $stubCommentedEntityIds;
/** /**
* Builds an comment entity destination. * Builds an comment entity destination.
* *
@ -48,10 +64,13 @@ class EntityComment extends EntityContentBase {
* The entity manager service. * The entity manager service.
* @param \Drupal\Core\State\StateInterface $state * @param \Drupal\Core\State\StateInterface $state
* The state storage object. * The state storage object.
* @param \Drupal\Core\Entity\Query\QueryFactory $entity_query
* The query object that can query the given entity type.
*/ */
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, EntityStorageInterface $storage, array $bundles, EntityManagerInterface $entity_manager, StateInterface $state) { public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, EntityStorageInterface $storage, array $bundles, EntityManagerInterface $entity_manager, StateInterface $state, QueryFactory $entity_query) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $storage, $bundles, $entity_manager); parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $storage, $bundles, $entity_manager);
$this->state = $state; $this->state = $state;
$this->entityQuery = $entity_query;
} }
/** /**
@ -67,7 +86,8 @@ class EntityComment extends EntityContentBase {
$container->get('entity.manager')->getStorage($entity_type), $container->get('entity.manager')->getStorage($entity_type),
array_keys($container->get('entity.manager')->getBundleInfo($entity_type)), array_keys($container->get('entity.manager')->getBundleInfo($entity_type)),
$container->get('entity.manager'), $container->get('entity.manager'),
$container->get('state') $container->get('state'),
$container->get('entity.query')
); );
} }
@ -85,4 +105,37 @@ class EntityComment extends EntityContentBase {
return $return; return $return;
} }
/**
* {@inheritdoc}
*/
protected function processStubRow(Row $row) {
parent::processStubRow($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);
$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);
$row->setSourceProperty($id_key, $this->stubCommentedEntityIds[$stub_commented_entity_type]);
}
else {
throw new MigrateException(t('Could not find parent entity to use for comment %id', ['%id' => implode(':', $row->getSourceIdValues())]), MigrationInterface::MESSAGE_ERROR);
}
}
$row->setDestinationProperty('entity_id', $this->stubCommentedEntityIds[$stub_commented_entity_type]);
$row->setDestinationProperty('entity_type', $stub_commented_entity_type);
$row->setDestinationProperty('created', REQUEST_TIME);
$row->setDestinationProperty('changed', REQUEST_TIME);
}
} }

View File

@ -19,7 +19,10 @@ class MigrateCommentTest extends MigrateDrupal6TestBase {
use CommentTestTrait; use CommentTestTrait;
static $modules = array('node', 'comment', 'text', 'filter'); // Directly testing that comments' entity_id is populated upon importing is
// not straightforward, but RDF module serves as an implicit test -
// its hook_comment_storage_load() references a stubbed comment.
static $modules = ['node', 'comment', 'text', 'filter', 'rdf'];
/** /**
* {@inheritdoc} * {@inheritdoc}
@ -30,8 +33,12 @@ class MigrateCommentTest extends MigrateDrupal6TestBase {
$this->installEntitySchema('node'); $this->installEntitySchema('node');
$this->installEntitySchema('comment'); $this->installEntitySchema('comment');
$this->installSchema('comment', ['comment_entity_statistics']); $this->installSchema('comment', ['comment_entity_statistics']);
$this->installSchema('system', ['router']);
$this->installConfig(['node', 'comment']); $this->installConfig(['node', 'comment']);
// The entity.node.canonical route must exist when the RDF hook is called.
$this->container->get('router.builder')->rebuild();
entity_create('node_type', array('type' => 'page'))->save(); entity_create('node_type', array('type' => 'page'))->save();
entity_create('node_type', array('type' => 'story'))->save(); entity_create('node_type', array('type' => 'story'))->save();
$this->addDefaultCommentField('node', 'story'); $this->addDefaultCommentField('node', 'story');

View File

@ -113,12 +113,11 @@ abstract class Entity extends DestinationBase implements ContainerFactoryPluginI
$this->updateEntity($entity, $row); $this->updateEntity($entity, $row);
} }
else { else {
$values = $row->getDestination(); // Stubs might need some required fields filled in.
// Stubs might not have the bundle specified.
if ($row->isStub()) { if ($row->isStub()) {
$values = $this->processStubValues($values); $this->processStubRow($row);
} }
$entity = $this->storage->create($values); $entity = $this->storage->create($row->getDestination());
$entity->enforceIsNew(); $entity->enforceIsNew();
} }
return $entity; return $entity;
@ -139,21 +138,14 @@ abstract class Entity extends DestinationBase implements ContainerFactoryPluginI
/** /**
* Process the stub values. * Process the stub values.
* *
* @param array $values * @param \Drupal\migrate\Row $row
* An array of destination values. * The row of data.
*
* @return array
* The processed stub values.
*/ */
protected function processStubValues(array $values) { protected function processStubRow(Row $row) {
$values = array_intersect_key($values, $this->getIds());
$bundle_key = $this->getKey('bundle'); $bundle_key = $this->getKey('bundle');
if ($bundle_key && !isset($values[$bundle_key])) { if ($bundle_key && empty($row->getDestinationProperty($bundle_key))) {
$values[$bundle_key] = reset($this->bundles); $row->setDestinationProperty($bundle_key, reset($this->bundles));
} }
return $values;
} }
/** /**