-
Notifications
You must be signed in to change notification settings - Fork 2
/
varbase_media.tokens.inc
152 lines (124 loc) · 4.68 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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?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().
*/
function varbase_media_token_info() {
$info = [];
$info['tokens']['media']['social_large'] = [
'name' => t('Social Large'),
'description' => t("Social Large (1200x630) image for the selected media type."),
'module' => 'media',
'type' => 'url',
];
$info['tokens']['media']['social_medium'] = [
'name' => t('Social Medium'),
'description' => t("Social Medium (600x315) image for the selected media type."),
'module' => 'media',
'type' => 'url',
];
$info['tokens']['media']['social_small'] = [
'name' => t('Social Small'),
'description' => t("Social Small (280x150) image for the selected media type."),
'module' => 'media',
'type' => 'url',
];
// Define the new 'share-image' token type.
$info['types']['share-image'] = [
'name' => t('Share Image'),
'description' => t('The social share image for the node. Checks field_media, field_image, field_video, and falls back to the theme default image if none are found.'),
'needs-data' => 'node',
'nested' => TRUE,
];
// Define tokens for each field.
$info['tokens']['share-image']['field_media'] = [
'name' => t('Share Image from field_media'),
'description' => t('The share image from field_media, if available.'),
'type' => 'media',
];
$info['tokens']['share-image']['field_image'] = [
'name' => t('Share Image from field_image'),
'description' => t('The share image from field_image, if available.'),
'type' => 'media',
];
$info['tokens']['share-image']['field_video'] = [
'name' => t('Share Image from field_video'),
'description' => t('The share image from field_video, if available.'),
'type' => 'media',
];
// Add share-image to node.
$info['tokens']['node']['share-image'] = [
'name' => t('Share Image'),
'description' => t('The social share image for the node. Checks field_media, field_image, field_video, and falls back to the theme default image if none are found.'),
'type' => 'share-image',
];
return $info;
}
/**
* 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;
}
}
}
if ($type === 'node' && !empty($data['node'])) {
/** @var \Drupal\node\Entity\Node $node */
$node = $data['node'];
$replacements = [];
foreach ($tokens as $token_name => $original) {
// When only using the smart [node:share-image] token.
if ($token_name == 'share-image') {
$replacements[$original] = varbase_media__get_node_share_image_url($node, 'social_large');
}
// When the the smart [node:share-image:#######:#####] has extra specific options.
elseif ($token_name == 'share-image:') {
// Get the array of token options.
$token_options = explode(":", $token_name);
// Get the field name.
$field_name = '';
if (isset($token_options[1]) && $token_options[1] != '') {
$field_name = $token_options[1];
}
// Get the social image style name.
$style_name = 'social_large';
if (isset($token_options[2]) && $token_options[2] != '') {
$style_name = $token_options[2];
}
$replacements[$original] = varbase_media__get_node_share_image_url($node, $style_name, $field_name);
}
}
}
return $replacements;
}