Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / migrate / src / Plugin / migrate / destination / EntityRevision.php
1 <?php
2
3 namespace Drupal\migrate\Plugin\migrate\destination;
4
5 use Drupal\Core\Entity\ContentEntityInterface;
6 use Drupal\migrate\MigrateException;
7 use Drupal\migrate\Row;
8
9 /**
10  * Provides entity revision destination plugin.
11  *
12  * @MigrateDestination(
13  *   id = "entity_revision",
14  *   deriver = "Drupal\migrate\Plugin\Derivative\MigrateEntityRevision"
15  * )
16  */
17 class EntityRevision extends EntityContentBase {
18
19   /**
20    * {@inheritdoc}
21    */
22   protected static function getEntityTypeId($plugin_id) {
23     // Remove entity_revision:
24     return substr($plugin_id, 16);
25   }
26
27   /**
28    * Gets the entity.
29    *
30    * @param \Drupal\migrate\Row $row
31    *   The row object.
32    * @param array $old_destination_id_values
33    *   The old destination IDs.
34    *
35    * @return \Drupal\Core\Entity\EntityInterface|false
36    *   The entity or false if it can not be created.
37    */
38   protected function getEntity(Row $row, array $old_destination_id_values) {
39     $revision_id = $old_destination_id_values ?
40       reset($old_destination_id_values) :
41       $row->getDestinationProperty($this->getKey('revision'));
42     if (!empty($revision_id) && ($entity = $this->storage->loadRevision($revision_id))) {
43       $entity->setNewRevision(FALSE);
44     }
45     else {
46       $entity_id = $row->getDestinationProperty($this->getKey('id'));
47       $entity = $this->storage->load($entity_id);
48
49       // If we fail to load the original entity something is wrong and we need
50       // to return immediately.
51       if (!$entity) {
52         return FALSE;
53       }
54
55       $entity->enforceIsNew(FALSE);
56       $entity->setNewRevision(TRUE);
57     }
58     $this->updateEntity($entity, $row);
59     $entity->isDefaultRevision(FALSE);
60     return $entity;
61   }
62
63   /**
64    * {@inheritdoc}
65    */
66   protected function save(ContentEntityInterface $entity, array $old_destination_id_values = []) {
67     $entity->save();
68     return [$entity->getRevisionId()];
69   }
70
71   /**
72    * {@inheritdoc}
73    */
74   public function getIds() {
75     if ($key = $this->getKey('revision')) {
76       return [$key => $this->getDefinitionFromEntity($key)];
77     }
78     throw new MigrateException('This entity type does not support revisions.');
79   }
80
81 }