function commerce_order_tokens in Commerce Core 8.2
Same name and namespace in other branches
- 7 modules/order/commerce_order.tokens.inc \commerce_order_tokens()
Implements hook_tokens().
File
- modules/
order/ commerce_order.tokens.inc, line 37 - Builds placeholder replacement tokens for order data.
Code
function commerce_order_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
$replacements = [];
if ($type === 'commerce_order' && !empty($data['commerce_order'])) {
$url_options = [
'absolute' => TRUE,
];
if (isset($options['langcode'])) {
$url_options['language'] = \Drupal::languageManager()
->getLanguage($options['langcode']);
}
$order = $data['commerce_order'];
assert($order instanceof OrderInterface);
foreach ($tokens as $name => $original) {
switch ($name) {
case 'url':
$url = Url::fromRoute('entity.commerce_order.user_view', [
'commerce_order' => $order
->id(),
'user' => $order
->getCustomerId(),
], $url_options)
->toString(TRUE);
assert($url instanceof GeneratedUrl);
$bubbleable_metadata
->addCacheableDependency($url);
$replacements[$original] = $url
->getGeneratedUrl();
break;
case 'admin-url':
$url = $order
->toUrl('canonical', $url_options)
->toString(TRUE);
assert($url instanceof GeneratedUrl);
$bubbleable_metadata
->addCacheableDependency($url);
$replacements[$original] = $url
->getGeneratedUrl();
break;
}
}
}
return $replacements;
}