From fa29a00fd4103c3764884c749231ef8f8a52b040 Mon Sep 17 00:00:00 2001 From: astanley Date: Thu, 24 Apr 2025 15:22:34 +0000 Subject: [PATCH] added linkage hooks --- caption_linker.module | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/caption_linker.module b/caption_linker.module index 493aebb..28c44cd 100644 --- a/caption_linker.module +++ b/caption_linker.module @@ -2,5 +2,41 @@ /** * @file - * Primary module hooks for Caption Linker module. */ + +use Drupal\Core\Entity\EntityInterface; +use Drupal\media\MediaInterface; +use Drupal\node\NodeInterface; + +/** + * Implements hook_entity_insert(). + */ +function caption_linker_entity_insert(EntityInterface $entity): void { + if ($entity instanceof MediaInterface) { + _caption_linker_link_caption_if_possible($entity); + } +} + +/** + * Implements hook_entity_update(). + */ +function caption_linker_entity_update(EntityInterface $entity): void { + if ($entity instanceof MediaInterface) { + _caption_linker_link_caption_if_possible($entity); + } +} + +/** + * Tries to link a caption if applicable. + */ +function _caption_linker_link_caption_if_possible(MediaInterface $media): void { + /** @var \Drupal\islandora\IslandoraUtils $islandora_utils */ + $islandora_utils = \Drupal::service('islandora.utils'); + /** @var \Drupal\caption_linker\Service\CaptionLinker $caption_linker */ + $caption_linker = \Drupal::service('caption_linker.caption_linker'); + + $node = $islandora_utils->getParentNode($media); + if ($node instanceof NodeInterface) { + $caption_linker->linkCaption($node); + } +}