3 namespace Drupal\entity_reference_revisions\Plugin\migrate\destination;
5 use Drupal\Core\Entity\ContentEntityInterface;
6 use Drupal\Core\TypedData\TranslatableInterface;
7 use Drupal\migrate\MigrateException;
8 use Drupal\migrate\Plugin\migrate\destination\EntityRevision;
9 use Drupal\migrate\Plugin\MigrateIdMapInterface;
10 use Drupal\migrate\Row;
13 * Provides entity_reference_revisions destination plugin.
15 * @MigrateDestination(
16 * id = "entity_reference_revisions",
17 * deriver = "Drupal\entity_reference_revisions\Plugin\Derivative\MigrateEntityReferenceRevisions"
20 class EntityReferenceRevisions extends EntityRevision {
25 protected static function getEntityTypeId($pluginId) {
26 // Remove "entity_reference_revisions:".
27 // Ideally, we would call getDerivativeId(), but since this is static
28 // that is not possible so we follow the same pattern as core.
29 return substr($pluginId, 27);
35 protected function save(ContentEntityInterface $entity, array $oldDestinationIdValues = array()) {
39 $this->getKey('id') => $entity->id(),
40 $this->getKey('revision') => $entity->getRevisionId(),
47 public function getIds() {
48 if ($revision_key = $this->getKey('revision')) {
49 $id_key = $this->getKey('id');
50 $ids[$id_key]['type'] = 'integer';
52 // TODO: Improve after https://www.drupal.org/node/2783715 is finished.
53 $ids[$revision_key]['type'] = 'integer';
55 if ($this->isTranslationDestination()) {
56 if ($revision_key = $this->getKey('langcode')) {
57 $ids[$revision_key]['type'] = 'string';
60 throw new MigrateException('This entity type does not support translation.');
66 throw new MigrateException('This entity type does not support revisions.');
72 protected function getEntity(Row $row, array $oldDestinationIdValues) {
73 $revision_id = $oldDestinationIdValues ?
74 array_pop($oldDestinationIdValues) :
75 $row->getDestinationProperty($this->getKey('revision'));
76 if (!empty($revision_id) && ($entity = $this->storage->loadRevision($revision_id))) {
77 $entity->setNewRevision(FALSE);
80 // Attempt to ensure we always have a bundle.
81 if ($bundle = $this->getBundle($row)) {
82 $row->setDestinationProperty($this->getKey('bundle'), $bundle);
85 // Stubs might need some required fields filled in.
87 $this->processStubRow($row);
89 $entity = $this->storage->create($row->getDestination())
91 $entity->setNewRevision(TRUE);
93 $entity = $this->updateEntity($entity, $row) ?: $entity;
94 $this->rollbackAction = MigrateIdMapInterface::ROLLBACK_DELETE;
101 public function rollback(array $destination_identifiers) {
102 if ($this->isTranslationDestination()) {
103 $this->rollbackTranslation($destination_identifiers);
106 $this->rollbackNonTranslation($destination_identifiers);
111 * Rollback translation destinations.
113 * @param array $destination_identifiers
114 * The IDs of the destination object to delete.
116 protected function rollbackTranslation(array $destination_identifiers) {
117 $entity = $this->storage->loadRevision(array_pop($destination_identifiers));
118 if ($entity && $entity instanceof TranslatableInterface) {
119 if ($key = $this->getKey('langcode')) {
120 if (isset($destination_identifier[$key])) {
121 $langcode = $destination_identifier[$key];
122 if ($entity->hasTranslation($langcode)) {
123 // Make sure we don't remove the default translation.
124 $translation = $entity->getTranslation($langcode);
125 if (!$translation->isDefaultTranslation()) {
126 $entity->removeTranslation($langcode);
136 * Rollback non-translation destinations.
138 * @param array $destination_identifiers
139 * The IDs of the destination object to delete.
141 protected function rollbackNonTranslation(array $destination_identifiers) {
142 $revision_id = array_pop($destination_identifiers);
143 $entity = $this->storage->loadRevision($revision_id);
145 if ($entity->isDefaultRevision()) {
149 $this->storage->deleteRevision($revision_id);