diff --git a/caption_linker.info.yml b/caption_linker.info.yml new file mode 100644 index 0000000..2bf0c28 --- /dev/null +++ b/caption_linker.info.yml @@ -0,0 +1,7 @@ +name: 'Caption Linker' +type: module +description: 'Links Ableplayer media to audio and video media' +package: Custom +core_version_requirement: ^10 || ^11 +dependencies: + - islandora:islandora diff --git a/caption_linker.module b/caption_linker.module new file mode 100644 index 0000000..493aebb --- /dev/null +++ b/caption_linker.module @@ -0,0 +1,6 @@ +get('entity_type.manager'), + $container->get('islandora.utils'), + $container->get('logger.channel.islandora'), + ); + } + + /** + * {@inheritdoc} + */ + public function access($object, ?AccountInterface $account = NULL, $return_as_object = FALSE) { + /** @var \Drupal\node\NodeInterface $object */ + $access = $object->access('update', $account, TRUE) + ->andIf($object->status->access('edit', $account, TRUE)); + + return $return_as_object ? $access : $access->isAllowed(); + } + + /** + * {@inheritdoc} + */ + public function execute(ContentEntityInterface $entity = NULL): void { + $transcript_media = $this->getMediaByUri('http://pcdm.org/use#Transcript'); + $host_media = $this->getMediaByUri('http://pcdm.org/use#ServiceFile') + ?? $this->getMediaByUri('http://pcdm.org/use#OriginalFile'); + + if ($transcript_media && $host_media + && $transcript_media->bundle() === 'able_player_caption' + && $host_media->hasField('field_ableplayer_media_caption') + && $host_media->get('field_ableplayer_media_caption')->isEmpty()) { + + $host_media->set('field_ableplayer_media_caption', ['target_id' => $transcript_media->id()]); + $host_media->save(); + } + } + + /** + * {@inheritdoc} + */ + public function getMediaByUri($entity, $uri) { + $term = $this->islandoraUtils->getTermForUri($uri); + return $this->islandoraUtils->getMediaWithTerm($entity, $term); + } + +}