-
Notifications
You must be signed in to change notification settings - Fork 0
/
islandora_tokens.module
120 lines (104 loc) · 3.8 KB
/
islandora_tokens.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
/**
* @file
* Provide tokens for Repository Item Thumbnail Image URL and Alternative Text.
*
* @todo:
* - Add tokens for media-service-file:url, media-service-file:alt, media-original-file:alt, media-original-file:mimetype
*/
use Drupal\islandora\IslandoraUtils;
use Drupal\Core\Render\BubbleableMetadata;
/**
* Implements hook_token_info().
*/
function islandora_tokens_token_info() {
$info['tokens']['node']['media-thumbnail-image:url'] = [
'name' => t('Media: Thumbnail Image URL.'),
'description' => t('URL of Thumbnail Image associated with Islandora Object via Media.'),
];
$info['tokens']['node']['media-thumbnail-image:alt'] = [
'name' => t('Alternative text for Media: Thumbnail Image.'),
'description' => t('Alternative text for Thumbnail Image associated with Islandora Object via Media.'),
];
// Deprecated in favour if hyphenated version.
$info['tokens']['node']['media_thumbnail_image:url'] = [
'name' => t('Media: Thumbnail Image URL.'),
'description' => t('Deprecated: URL of Thumbnail Image associated with Islandora Object via Media.'),
];
// Deprecated in favour if hyphenated version.
$info['tokens']['node']['media_thumbnail_image:alt'] = [
'name' => t('Alternative text for Media: Thumbnail Image.'),
'description' => t('Deprecated: Alternative text for Thumbnail Image associated with Islandora Object via Media.'),
];
return $info;
}
/**
* Implements hook_tokens().
*/
function islandora_tokens_tokens($type, $tokens, array $data, array $options, \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata) {
$replacements = [];
if ($type == 'node' && !empty($data['node'])) {
if (!is_array($tokens) || empty($tokens)) {
\Drupal::logger('islandora_tokens')
->alert('Tokens not correct format: @tokens', [
'@tokens' => print_r($tokens, 1),
]);
return;
}
$islandoraUtils = \Drupal::service('islandora.utils');
foreach ($tokens as $name => $original) {
switch ($name) {
case 'media-thumbnail-image:url':
case 'media_thumbnail_image:url':
$term = $islandoraUtils->getTermForUri('http://pcdm.org/use#ThumbnailImage');
$media = $islandoraUtils->getMediaWithTerm($data['node'], $term);
// Is there media?
// @todo: is this single or multiple?
if ($media) {
$file = islandora_tokens_image_from_media($media);
if (!empty($file)) {
$url = $file->getFileUri();
$replacements[$original] = $url;
}
}
break;
case 'media-thumbnail-image:alt':
case 'media_thumbnail_image:alt':
$alt = '';
$term = $islandoraUtils->getTermForUri('http://pcdm.org/use#ThumbnailImage');
$media = $islandoraUtils->getMediaWithTerm($data['node'], $term);
// Is there media?
// @todo: is this single or multiple?
if ($media) {
// Is the media an image?
if (isset($media->field_media_image)) {
$alt = $media->field_media_image[0]->alt;
}
}
// @todo: get alt from original or service file, if thumbnail alt is empty.
$replacements[$original] = $alt;
break;
}
}
}
return $replacements;
}
/**
* Get File referenced by given Media.
*
* @todo: Add to IslandoraUtils?
* @todo: Had to remove typehint MediaInterface from function.
*
* @param \Drupal\media\MediaInterface $media
* The Media to operate on.
*
* @return mixed
* \Drupal\file\FileInterface on success, NULL otherwise.
*/
function islandora_tokens_image_from_media($media) {
$files = [];
if ($media->hasField('field_media_image')) {
$files = $media->get('field_media_image')->referencedEntities();
}
return !empty($files) ? reset($files) : NULL;
}