function uc_order_tokens in Ubercart 8.4
Same name and namespace in other branches
- 7.3 uc_order/uc_order.tokens.inc \uc_order_tokens()
Implements hook_tokens().
File
- uc_order/
uc_order.tokens.inc, line 142 - Token hooks for the uc_order module.
Code
function uc_order_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
$token_service = \Drupal::token();
if (isset($options['langcode'])) {
$langcode = $options['langcode'];
}
else {
$langcode = LanguageInterface::LANGCODE_DEFAULT;
}
$replacements = [];
if ($type == 'uc_order' && !empty($data['uc_order'])) {
/** @var \Drupal\uc_order\OrderInterface $order */
$order = $data['uc_order'];
$path = 'user/' . $order
->getOwnerId() . '/orders/' . $order
->id();
$admin_path = 'admin/store/orders/' . $order
->id();
foreach ($tokens as $name => $original) {
switch ($name) {
case 'new-username':
if (isset($order->data->new_user_name)) {
$replacements[$original] = $order->data->new_user_name;
}
break;
case 'new-password':
$replacements[$original] = isset($order->password) ? $order->password : t('Your password');
break;
case 'order-number':
$replacements[$original] = $order
->id();
break;
case 'url':
$replacements[$original] = Url::fromUri('internal:/' . $path, [
'absolute' => TRUE,
])
->toString();
break;
case 'link':
$replacements[$original] = Link::fromTextAndUrl($order
->id(), Url::fromUri('internal:/' . $path, [
'absolute' => TRUE,
]))
->toString();
break;
case 'admin-url':
$replacements[$original] = Url::fromUri('internal:/' . $admin_path, [
'absolute' => TRUE,
])
->toString();
break;
case 'admin-link':
$replacements[$original] = Link::fromTextAndUrl($order
->id(), Url::fromUri('internal:/' . $admin_path, [
'absolute' => TRUE,
]))
->toString();
break;
case 'subtotal':
$subtotal = '';
if (is_array($order->line_items)) {
foreach ($order->line_items as $key => $value) {
if ($value['type'] == 'subtotal') {
$subtotal = uc_currency_format($order->line_items[$key]['amount']);
}
}
}
$replacements[$original] = $subtotal;
break;
case 'subtotal-value':
if (is_array($order->line_items)) {
foreach ($order->line_items as $key => $value) {
if ($value['type'] == 'subtotal') {
$subtotal = $order->line_items[$key]['amount'];
}
}
}
$replacements[$original] = $subtotal;
break;
case 'total':
$replacements[$original] = uc_currency_format($order
->getTotal());
break;
case 'total-value':
$replacements[$original] = $order
->getTotal();
break;
case 'email':
case 'mail':
$replacements[$original] = $order
->getEmail();
break;
case 'shipping-method':
if (is_array($order->line_items)) {
foreach ($order->line_items as $value) {
if ($value['type'] == 'shipping' && !isset($ship_method)) {
$ship_method = $value['title'];
break;
}
}
}
$replacements[$original] = !isset($ship_method) ? t('Standard delivery') : $ship_method;
break;
case 'shipping-address':
// Cast Address object to string to get country-specific formatting.
$address = [
'#markup' => (string) $order
->getAddress('delivery'),
];
$replacements[$original] = drupal_render($address);
break;
case 'shipping-phone':
$replacements[$original] = $order
->getAddress('delivery')
->getPhone();
break;
case 'billing-address':
// Cast Address object to string to get country-specific formatting.
$address = [
'#markup' => (string) $order
->getAddress('billing'),
];
$replacements[$original] = drupal_render($address);
break;
case 'billing-phone':
$replacements[$original] = $order
->getAddress('billing')
->getPhone();
break;
case 'first-name':
if (\Drupal::config('uc_store.settings')
->get('customer_address') == 'shipping') {
$replacements[$original] = $order
->getAddress('delivery')
->getFirstName();
}
else {
$replacements[$original] = $order
->getAddress('billing')
->getFirstName();
}
break;
case 'last-name':
if (\Drupal::config('uc_store.settings')
->get('customer_address') == 'shipping') {
$replacements[$original] = $order
->getAddress('delivery')
->getLastName();
}
else {
$replacements[$original] = $order
->getAddress('billing')
->getLastName();
}
break;
case 'comments':
$result = db_query_range("SELECT message FROM {uc_order_comments} WHERE order_id = :order_id AND uid = :uid ORDER BY created DESC", 0, 1, [
':order_id' => $order
->id(),
':uid' => 0,
])
->fetchField();
$replacements[$original] = empty($result) ? t('<i>No comments left.</i>') : $result;
break;
case 'last-comment':
$result = db_query_range("SELECT message FROM {uc_order_comments} WHERE order_id = :order_id AND uid > :uid ORDER BY created DESC", 0, 1, [
':order_id' => $order
->id(),
':uid' => 0,
])
->fetchField();
$replacements[$original] = empty($result) ? t('<i>No comment found.</i>') : $result;
break;
case 'order-status':
$replacements[$original] = $order
->getStatus()
->getName();
break;
case 'customer':
$replacements[$original] = $order
->getOwnerId();
break;
case 'created':
$date_format = DateFormat::load('short');
$bubbleable_metadata
->addCacheableDependency($date_format);
$replacements[$original] = \Drupal::service('date.formatter')
->format($order
->getCreatedTime(), 'short', '', NULL, $langcode);
break;
case 'changed':
$date_format = DateFormat::load('short');
$bubbleable_metadata
->addCacheableDependency($date_format);
$replacements[$original] = \Drupal::service('date.formatter')
->format($order
->getChangedTime(), 'short', '', NULL, $langcode);
break;
case 'products':
$products = [];
foreach ($order->products as $product) {
$qty = [
'#theme' => 'uc_qty',
'#qty' => $product->qty->value,
];
$products[] = drupal_render($qty) . ' ' . $product->title->value;
}
$replacements[$original] = implode("<br />\n", $products);
break;
}
}
// Handles chaining for tokens that have 'type' defined in hook_token_info()
if ($link_tokens = $token_service
->findWithPrefix($tokens, 'url')) {
$replacements += $token_service
->generate('url', $link_tokens, [
'path' => $path,
], $options, $bubbleable_metadata);
}
if ($link_tokens = $token_service
->findWithPrefix($tokens, 'admin-url')) {
$replacements += $token_service
->generate('url', $link_tokens, [
'path' => $admin_path,
], $options, $bubbleable_metadata);
}
if ($customer_tokens = $token_service
->findWithPrefix($tokens, 'customer')) {
$replacements += $token_service
->generate('user', $customer_tokens, [
'user' => $order
->getOwner(),
], $options, $bubbleable_metadata);
}
if ($created_tokens = $token_service
->findWithPrefix($tokens, 'created')) {
$replacements += $token_service
->generate('date', $created_tokens, [
'date' => $order
->getCreatedTime(),
], $options, $bubbleable_metadata);
}
if ($changed_tokens = $token_service
->findWithPrefix($tokens, 'changed')) {
$replacements += $token_service
->generate('date', $changed_tokens, [
'date' => $order
->getChangedTime(),
], $options, $bubbleable_metadata);
}
}
return $replacements;
}