From e9599f7a81fba3bb5c39a8f6512fde09e4f16608 Mon Sep 17 00:00:00 2001 From: Eli Zoller Date: Fri, 1 Nov 2019 12:56:33 -0700 Subject: [PATCH] different method of checking for media revisions --- src/EventGenerator/EventGenerator.php | 32 +++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/src/EventGenerator/EventGenerator.php b/src/EventGenerator/EventGenerator.php index 7fc0a357..eb040755 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. @@ -102,6 +104,7 @@ class EventGenerator implements EventGeneratorInterface { if ($entity->getEntityType()->isRevisionable()) { \Drupal::logger('islandora')->notice('its revisionable'); $isNewRev = $this->isNewRevision($entity); + \Drupal::logger('is new revision')->notice($isNewRev); } $event["object"]["isNewVersion"] = $isNewRev; @@ -161,8 +164,33 @@ class EventGenerator implements EventGeneratorInterface { * Is new version. */ protected function isNewRevision(EntityInterface $entity) { - $revision_ids = \Drupal::entityTypeManager()->getStorage($entity->getEntityTypeId())->revisionIds($entity); - return count($revision_ids) > 1; + 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') + ->pager(50) + ->execute(); + // \Drupal::logger('revision array')->notice('
' . print_r(array_keys($result), TRUE) . '
'); + return array_keys($result); } }