You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
70 lines
1.6 KiB
70 lines
1.6 KiB
<?php |
|
|
|
/** |
|
* @file |
|
* Caption Linker module hooks. |
|
*/ |
|
|
|
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 { |
|
_caption_linker_handle_media_change($entity); |
|
} |
|
|
|
/** |
|
* Implements hook_entity_update(). |
|
*/ |
|
function caption_linker_entity_update(EntityInterface $entity): void { |
|
_caption_linker_handle_media_change($entity); |
|
} |
|
|
|
/** |
|
* Shared handler for media insert/update. |
|
*/ |
|
function _caption_linker_handle_media_change(EntityInterface $entity): void { |
|
if (!$entity instanceof MediaInterface) { |
|
return; |
|
} |
|
|
|
if (!_caption_linker_is_hook_enabled()) { |
|
return; |
|
} |
|
|
|
_caption_linker_link_caption_if_possible($entity); |
|
} |
|
|
|
/** |
|
* Returns TRUE if caption linking via hooks is enabled in config. |
|
*/ |
|
function _caption_linker_is_hook_enabled(): bool { |
|
static $enabled; |
|
|
|
if (!isset($enabled)) { |
|
$enabled = (bool) \Drupal::config('caption_linker.settings')->get('use_hook'); |
|
} |
|
|
|
return $enabled; |
|
} |
|
|
|
/** |
|
* 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) { |
|
return; |
|
} |
|
|
|
$caption_linker->linkCaption($node); |
|
}
|
|
|