-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvarbase_media.tokens.inc
81 lines (66 loc) · 2.27 KB
/
varbase_media.tokens.inc
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
<?php
/**
* @file
* Contains varbase_media.tokens.inc.
*/
/**
* To have all Varbase Media general and global tokens.
*/
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\Language\LanguageInterface;
/**
* Implements hook_token_info_alter().
*/
function varbase_media_token_info_alter(&$data) {
$data['tokens']['media']['social_large'] = [
'name' => t('Social Large'),
'description' => t("Social Large (1200x630) image for the selected media type."),
'module' => 'media',
'type' => 'url',
];
$data['tokens']['media']['social_medium'] = [
'name' => t('Social Medium'),
'description' => t("Social Medium (600x315) image for the selected media type."),
'module' => 'media',
'type' => 'url',
];
$data['tokens']['media']['social_small'] = [
'name' => t('Social Small'),
'description' => t("Social Small (280x150) image for the selected media type."),
'module' => 'media',
'type' => 'url',
];
}
/**
* Implements hook_tokens().
*/
function varbase_media_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
if (isset($options['langcode'])) {
$langcode = $options['langcode'];
}
else {
$langcode = LanguageInterface::LANGCODE_DEFAULT;
}
$replacements = [];
if ($type === 'media' && !empty($data['media'])) {
/** @var \Drupal\media\MediaInterface $media_entity */
$media_entity = \Drupal::service('entity.repository')->getTranslationFromContext($data['media'], $langcode, ['operation' => 'media_entity_tokens']);
foreach ($tokens as $token_name => $original) {
switch ($token_name) {
// Social Large (1200x630) image for the selected media type.
case 'social_large':
$replacements[$original] = varbase_media__image_url($media_entity, 'social_large');
break;
// Social Medium (600x315) image for the selected media type.
case 'social_medium':
$replacements[$original] = varbase_media__image_url($media_entity, 'social_medium');
break;
// Social Small (280x150) image for the selected media type.
case 'social_small':
$replacements[$original] = varbase_media__image_url($media_entity, 'social_small');
break;
}
}
}
return $replacements;
}