-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoe_subscriptions.tokens.inc
More file actions
55 lines (44 loc) · 1.34 KB
/
oe_subscriptions.tokens.inc
File metadata and controls
55 lines (44 loc) · 1.34 KB
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
<?php
/**
* @file
* Token callbacks for the Subscriptions module.
*/
declare(strict_types=1);
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\Url;
/**
* Implements hook_token_info().
*/
function oe_subscriptions_token_info(): array {
$info = [];
$info['tokens']['user']['subscriptions-page'] = [
'name' => t('Subscriptions page URL'),
'description' => t('The URL to subscriptions page.'),
'type' => 'url',
];
return $info;
}
/**
* Implements hook_tokens().
*/
function oe_subscriptions_tokens(string $type, array $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata): array {
$replacements = [];
if (
$type === 'user' &&
!empty($data['user'])
) {
$url = Url::fromRoute('oe_subscriptions.user.subscriptions', [
'user' => $data['user']->id(),
]);
if (isset($tokens['subscriptions-page'])) {
$result = $url->setAbsolute()->toString(TRUE);
$bubbleable_metadata->addCacheableDependency($result);
$replacements[$tokens['subscriptions-page']] = $result->getGeneratedUrl();
}
// Chained token relationships.
if ($url_tokens = \Drupal::token()->findWithPrefix($tokens, 'subscriptions-page')) {
$replacements += \Drupal::token()->generate('url', $url_tokens, ['url' => $url], $options, $bubbleable_metadata);
}
}
return $replacements;
}