From 00b9006c8b569199599eaccf90171c09b576b201 Mon Sep 17 00:00:00 2001 From: dannylamb Date: Tue, 12 Nov 2019 15:19:56 -0400 Subject: [PATCH] Versioning (#733) --- src/EventGenerator/EventGenerator.php | 47 +++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/src/EventGenerator/EventGenerator.php b/src/EventGenerator/EventGenerator.php index bf95e97d..8b8da0ca 100644 --- a/src/EventGenerator/EventGenerator.php +++ b/src/EventGenerator/EventGenerator.php @@ -6,6 +6,8 @@ use Drupal\Core\Entity\EntityInterface; use Drupal\islandora\IslandoraUtils; use Drupal\islandora\MediaSource\MediaSourceService; use Drupal\user\UserInterface; +use Drupal\media\Entity\Media; +use Drupal\Core\Entity\EntityStorageInterface; /** * The default EventGenerator implementation. @@ -99,6 +101,14 @@ class EventGenerator implements EventGeneratorInterface { $event["summary"] = ucfirst($data["event"]) . " a " . ucfirst($entity_type); } + if ($data['event'] != "Generate Derivative") { + $isNewRev = FALSE; + if ($entity->getEntityType()->isRevisionable()) { + $isNewRev = $this->isNewRevision($entity); + } + $event["object"]["isNewVersion"] = $isNewRev; + } + // Add REST links for non-file entities. if ($entity_type != 'file') { $event['object']['url'][] = [ @@ -145,4 +155,41 @@ class EventGenerator implements EventGeneratorInterface { return json_encode($event); } + /** + * Method to check if an entity is a new revision. + * + * @param \Drupal\Core\Entity\EntityInterface $entity + * Drupal Entity. + * + * @return bool + * Is new version. + */ + protected function isNewRevision(EntityInterface $entity) { + if ($entity->getEntityTypeId() == "node") { + $revision_ids = \Drupal::entityTypeManager()->getStorage($entity->getEntityTypeId())->revisionIds($entity); + return count($revision_ids) > 1; + } + elseif ($entity->getEntityTypeId() == "media") { + $mediaStorage = \Drupal::entityTypeManager()->getStorage($entity->getEntityTypeId()); + return count($this->getRevisionIds($entity, $mediaStorage)) > 1; + } + } + + /** + * Method to get the revisionIds of a media object. + * + * @param \Drupal\media\Entity\Media $media + * Media object. + * @param \Drupal\Core\Entity\EntityStorageInterface $media_storage + * Media Storage. + */ + protected function getRevisionIds(Media $media, EntityStorageInterface $media_storage) { + $result = $media_storage->getQuery() + ->allRevisions() + ->condition($media->getEntityType()->getKey('id'), $media->id()) + ->sort($media->getEntityType()->getKey('revision'), 'DESC') + ->execute(); + return array_keys($result); + } + }