utils = $utils; $this->mediaSource = $media_source; } /** * {@inheritdoc} */ public function generateEvent(EntityInterface $entity, UserInterface $user, array $data) { $user_url = $this->utils->getEntityUrl($user); $entity_type = $entity->getEntityTypeId(); if ($entity_type == 'file') { $entity_url = $this->utils->getDownloadUrl($entity); $mimetype = $entity->getMimeType(); } else { $entity_url = $this->utils->getEntityUrl($entity); $mimetype = 'text/html'; } $event = [ "@context" => "https://www.w3.org/ns/activitystreams", "actor" => [ "type" => "Person", "id" => "urn:uuid:{$user->uuid()}", "url" => [ [ "name" => "Canonical", "type" => "Link", "href" => "$user_url", "mediaType" => "text/html", "rel" => "canonical", ], ], ], "object" => [ "id" => "urn:uuid:{$entity->uuid()}", "url" => [ [ "name" => "Canonical", "type" => "Link", "href" => $entity_url, "mediaType" => $mimetype, "rel" => "canonical", ], ], ], ]; $entity_type = $entity->getEntityTypeId(); $event_type = $data["event"]; if ($data["event"] == "Generate Derivative") { $event["type"] = "Activity"; $event["summary"] = $data["event"]; } else { $event["type"] = ucfirst($data["event"]); $event["summary"] = ucfirst($data["event"]) . " a " . ucfirst($entity_type); } $isNewRev = FALSE; if ($entity->getEntityType()->isRevisionable()) { \Drupal::logger('islandora')->notice('its revisionable'); $isNewRev = $this->isNewRevision($entity); } $event["object"]["isNewVersion"] = $isNewRev; // Add REST links for non-file entities. if ($entity_type != 'file') { $event['object']['url'][] = [ "name" => "JSON", "type" => "Link", "href" => $this->utils->getRestUrl($entity, 'json'), "mediaType" => "application/json", "rel" => "alternate", ]; $event['object']['url'][] = [ "name" => "JSONLD", "type" => "Link", "href" => $this->utils->getRestUrl($entity, 'jsonld'), "mediaType" => "application/ld+json", "rel" => "alternate", ]; } // Add a link to the file described by a media. if ($entity_type == 'media') { $file = $this->mediaSource->getSourceFile($entity); if ($file) { $event['object']['url'][] = [ "name" => "Describes", "type" => "Link", "href" => $this->utils->getDownloadUrl($file), "mediaType" => $file->getMimeType(), "rel" => "describes", ]; } } unset($data["event"]); unset($data["queue"]); if (!empty($data)) { $event["attachment"] = [ "type" => "Object", "content" => $data, "mediaType" => "application/json", ]; } 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) { $revision_ids = \Drupal::entityTypeManager()->getStorage($entity->getEntityTypeId())->revisionIds($entity); return count($revision_ids) > 1; } }