Upgraded imagemagick and manually altered pdf to image module to handle changes....
[yaffs-website] / web / modules / contrib / entity_reference_revisions / src / Plugin / migrate / destination / EntityReferenceRevisions.php
1 <?php
2
3 namespace Drupal\entity_reference_revisions\Plugin\migrate\destination;
4
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;
11
12 /**
13  * Provides entity_reference_revisions destination plugin.
14  *
15  * @MigrateDestination(
16  *   id = "entity_reference_revisions",
17  *   deriver = "Drupal\entity_reference_revisions\Plugin\Derivative\MigrateEntityReferenceRevisions"
18  * )
19  */
20 class EntityReferenceRevisions extends EntityRevision {
21
22   /**
23    * {@inheritdoc}
24    */
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);
30   }
31
32   /**
33    * {@inheritdoc}
34    */
35   protected function save(ContentEntityInterface $entity, array $oldDestinationIdValues = array()) {
36     $entity->save();
37
38     return [
39       $this->getKey('id') => $entity->id(),
40       $this->getKey('revision') => $entity->getRevisionId(),
41     ];
42   }
43
44   /**
45    * {@inheritdoc}
46    */
47   public function getIds() {
48     if ($revision_key = $this->getKey('revision')) {
49       $id_key = $this->getKey('id');
50       $ids[$id_key]['type'] = 'integer';
51
52       // TODO: Improve after https://www.drupal.org/node/2783715 is finished.
53       $ids[$revision_key]['type'] = 'integer';
54
55       if ($this->isTranslationDestination()) {
56         if ($revision_key = $this->getKey('langcode')) {
57           $ids[$revision_key]['type'] = 'string';
58         }
59         else {
60           throw new MigrateException('This entity type does not support translation.');
61         }
62       }
63
64       return $ids;
65     }
66     throw new MigrateException('This entity type does not support revisions.');
67   }
68
69   /**
70    * {@inheritdoc}
71    */
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);
78     }
79     else {
80       // Attempt to ensure we always have a bundle.
81       if ($bundle = $this->getBundle($row)) {
82         $row->setDestinationProperty($this->getKey('bundle'), $bundle);
83       }
84
85       // Stubs might need some required fields filled in.
86       if ($row->isStub()) {
87         $this->processStubRow($row);
88       }
89       $entity = $this->storage->create($row->getDestination())
90         ->enforceIsNew(TRUE);
91       $entity->setNewRevision(TRUE);
92     }
93     $entity = $this->updateEntity($entity, $row) ?: $entity;
94     $this->rollbackAction = MigrateIdMapInterface::ROLLBACK_DELETE;
95     return $entity;
96   }
97
98   /**
99    * {@inheritdoc}
100    */
101   public function rollback(array $destination_identifiers) {
102     if ($this->isTranslationDestination()) {
103       $this->rollbackTranslation($destination_identifiers);
104     }
105     else {
106       $this->rollbackNonTranslation($destination_identifiers);
107     }
108   }
109
110   /**
111    * Rollback translation destinations.
112    *
113    * @param array $destination_identifiers
114    *   The IDs of the destination object to delete.
115    */
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);
127               $entity->save();
128             }
129           }
130         }
131       }
132     }
133   }
134
135   /**
136    * Rollback non-translation destinations.
137    *
138    * @param array $destination_identifiers
139    *   The IDs of the destination object to delete.
140    */
141   protected function rollbackNonTranslation(array $destination_identifiers) {
142     $revision_id = array_pop($destination_identifiers);
143     $entity = $this->storage->loadRevision($revision_id);
144     if ($entity) {
145       if ($entity->isDefaultRevision()) {
146         $entity->delete();
147       }
148       else {
149         $this->storage->deleteRevision($revision_id);
150       }
151     }
152   }
153 }