-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhelfi_platform_config.tokens.inc
120 lines (105 loc) · 3.46 KB
/
helfi_platform_config.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
<?php
/**
* @file
* Contains token data for helfi api base.
*/
declare(strict_types=1);
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\paragraphs\ParagraphInterface;
/**
* Implements hook_token_info().
*/
function helfi_platform_config_token_info() : array {
$info['tokens']['site']['shareable-image'] = [
'name' => t('Default OG Image'),
'description' => t('Default OG image is used as a default thumbnail in social networks and other services.'),
];
$info['tokens']['node']['shareable-image'] = [
'name' => t('Shareable image'),
'description' => t('Shareable image is used as a thumbnail in social networks and other services.'),
];
$info['tokens']['site']['page-title-suffix'] = [
'name' => t('Page title suffix'),
'description' => t('Official suffix for page title.'),
];
$info['tokens']['node']['lead-in'] = [
'name' => t('Lead in'),
'description' => t(
'Lead in will try to use the hero paragraph description if it exists. If not, it will use the node lead in field.'
),
];
return $info;
}
/**
* Implements hook_tokens().
*
* @see \Drupal\helfi_platform_config\Token\OGImageManager
*/
function helfi_platform_config_tokens(
$type,
$tokens,
array $data,
array $options,
BubbleableMetadata $bubbleable_metadata,
) : array {
$replacements = [];
foreach ($tokens as $name => $original) {
if ($name === 'shareable-image') {
$entity = $data[$type] ?? NULL;
if ($entity === NULL || $entity instanceof EntityInterface) {
/** @var \Drupal\helfi_platform_config\Token\OGImageManager $image_manager */
$image_manager = \Drupal::service('helfi_platform_config.og_image_manager');
$replacements[$original] = $image_manager->buildUrl($entity);
}
}
elseif ($name === 'page-title-suffix') {
$language = Drupal::languageManager()
->getCurrentLanguage(LanguageInterface::TYPE_INTERFACE);
$replacements[$original] = match ($language->getId()) {
'fi' => 'Helsingin kaupunki',
'sv' => 'Helsingfors stad',
'ru' => 'Гopoд Xeльcинки',
default => 'City of Helsinki',
};
}
// Custom token for lead in.
elseif ($name === 'lead-in' && !empty($data['node'])) {
/** @var \Drupal\node\NodeInterface $node */
$node = $data['node'];
$lead_in_text = '';
// Check if lead in field exists.
if (
$node->hasField('field_lead_in') &&
!$node?->get('field_lead_in')?->isEmpty()
) {
// Use lead in field as lead in text.
$lead_in_text = $node->get('field_lead_in')->value;
}
// Check if hero paragraph and hero paragraph description exists.
if (
$node->hasField('field_hero') &&
!$node->get('field_hero')?->first()?->isEmpty()
) {
// Get hero paragraph.
$hero = $node->get('field_hero')
?->first()
?->get('entity')
?->getTarget()
?->getValue();
if (
$hero instanceof ParagraphInterface &&
$hero->hasField('field_hero_desc') &&
!$hero->get('field_hero_desc')->isEmpty()
) {
// Use hero paragraph description as lead in text.
$lead_in_text = $hero->get('field_hero_desc')->value;
}
}
// Add lead in text to replacements.
$replacements[$original] = $lead_in_text;
}
}
return $replacements;
}