EntityReferenceRevisions.php in Entity Reference Revisions 8
File
src/Plugin/migrate/destination/EntityReferenceRevisions.php
View source
<?php
namespace Drupal\entity_reference_revisions\Plugin\migrate\destination;
use Drupal\Component\Plugin\ConfigurableInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\TypedData\TranslatableInterface;
use Drupal\migrate\MigrateException;
use Drupal\migrate\Plugin\migrate\destination\EntityRevision;
use Drupal\migrate\Plugin\MigrateIdMapInterface;
use Drupal\migrate\Row;
class EntityReferenceRevisions extends EntityRevision implements ConfigurableInterface {
public function setConfiguration(array $configuration) {
$this->configuration = $configuration;
}
public function getConfiguration() {
return $this->configuration;
}
public function defaultConfiguration() {
return [
'new_revisions' => FALSE,
];
}
protected static function getEntityTypeId($pluginId) {
return substr($pluginId, 27);
}
protected function save(ContentEntityInterface $entity, array $oldDestinationIdValues = []) {
$entity
->save();
return [
$this
->getKey('id') => $entity
->id(),
$this
->getKey('revision') => $entity
->getRevisionId(),
];
}
public function getIds() {
if ($revision_key = $this
->getKey('revision')) {
$id_key = $this
->getKey('id');
$ids[$id_key]['type'] = 'integer';
$ids[$revision_key]['type'] = 'integer';
if ($this
->isTranslationDestination()) {
if ($revision_key = $this
->getKey('langcode')) {
$ids[$revision_key]['type'] = 'string';
}
else {
throw new MigrateException('This entity type does not support translation.');
}
}
return $ids;
}
throw new MigrateException('This entity type does not support revisions.');
}
protected function getEntity(Row $row, array $oldDestinationIdValues) {
$entity_id = $oldDestinationIdValues ? array_shift($oldDestinationIdValues) : $this
->getEntityId($row);
$configuration = $this
->getConfiguration();
if (isset($configuration['force_revision']) && $configuration['force_revision'] == TRUE) {
$revision_id = NULL;
}
else {
$revision_id = $oldDestinationIdValues ? array_pop($oldDestinationIdValues) : $row
->getDestinationProperty($this
->getKey('revision'));
}
if (!empty($revision_id) && ($entity = $this->storage
->loadRevision($revision_id))) {
if (!empty($entity_id) && $entity
->id() != $entity_id) {
throw new MigrateException("The revision_id exists for this entity type, but does not belong to the given entity id");
}
$entity = $this
->updateEntity($entity, $row) ?: $entity;
}
elseif (!empty($entity_id) && ($entity = $this->storage
->load($entity_id))) {
if (!empty($this->configuration['new_revisions'])) {
$entity
->setNewRevision(TRUE);
}
$entity = $this
->updateEntity($entity, $row) ?: $entity;
}
else {
if ($bundle = $this
->getBundle($row)) {
$row
->setDestinationProperty($this
->getKey('bundle'), $bundle);
}
if ($row
->isStub()) {
$this
->processStubRow($row);
}
$entity = $this->storage
->create($row
->getDestination())
->enforceIsNew(TRUE);
$entity
->setNewRevision(TRUE);
}
$this->rollbackAction = MigrateIdMapInterface::ROLLBACK_DELETE;
return $entity;
}
public function rollback(array $destination_identifiers) {
if ($this
->isTranslationDestination()) {
$this
->rollbackTranslation($destination_identifiers);
}
else {
$this
->rollbackNonTranslation($destination_identifiers);
}
}
protected function rollbackTranslation(array $destination_identifiers) {
$entity = $this->storage
->loadRevision(array_pop($destination_identifiers));
if ($entity && $entity instanceof TranslatableInterface) {
if ($key = $this
->getKey('langcode')) {
if (isset($destination_identifiers[$key])) {
$langcode = $destination_identifiers[$key];
if ($entity
->hasTranslation($langcode)) {
$translation = $entity
->getTranslation($langcode);
if (!$translation
->isDefaultTranslation()) {
$entity
->removeTranslation($langcode);
$entity
->save();
}
}
}
}
}
}
protected function rollbackNonTranslation(array $destination_identifiers) {
$revision_id = array_pop($destination_identifiers);
$entity = $this->storage
->loadRevision($revision_id);
if ($entity) {
if ($entity
->isDefaultRevision()) {
$entity
->delete();
}
else {
$this->storage
->deleteRevision($revision_id);
}
}
}
}