function commerce_line_item_tokens in Commerce Core 7
Implements hook_tokens().
File
- modules/
line_item/ commerce_line_item.tokens.inc, line 69 - Builds placeholder replacement tokens for line item related data.
Code
function commerce_line_item_tokens($type, $tokens, array $data = array(), array $options = array()) {
$url_options = array(
'absolute' => TRUE,
);
if (isset($options['language'])) {
$url_options['language'] = $options['language'];
$language_code = $options['language']->language;
}
else {
$language_code = NULL;
}
$sanitize = !empty($options['sanitize']);
$replacements = array();
if ($type == 'commerce-line-item' && !empty($data['commerce-line-item'])) {
$line_item = $data['commerce-line-item'];
foreach ($tokens as $name => $original) {
switch ($name) {
// Simple key values on the line item.
case 'line-item-id':
$replacements[$original] = $line_item->line_item_id;
break;
case 'type':
$replacements[$original] = $sanitize ? check_plain($line_item->type) : $line_item->type;
break;
case 'type-name':
$replacements[$original] = $sanitize ? check_plain(commerce_line_item_type_get_name($line_item->type)) : commerce_line_item_type_get_name($line_item->type);
break;
case 'line-item-label':
$replacements[$original] = $sanitize ? check_plain($line_item->line_item_label) : $line_item->line_item_label;
break;
case 'quantity':
$replacements[$original] = $sanitize ? check_plain($line_item->quantity) : $line_item->quantity;
break;
// Default values for the chained tokens handled below.
case 'order':
if ($line_item->order_id) {
$order = commerce_order_load($line_item->order_id);
$replacements[$original] = $sanitize ? check_plain($order->order_number) : $order->order_number;
}
break;
case 'created':
$replacements[$original] = format_date($line_item->created, 'medium', '', NULL, $language_code);
break;
case 'changed':
$replacements[$original] = format_date($line_item->changed, 'medium', '', NULL, $language_code);
break;
}
}
if ($order_tokens = token_find_with_prefix($tokens, 'order')) {
$order = commerce_order_load($line_item->order_id);
$replacements += token_generate('commerce-order', $order_tokens, array(
'commerce-order' => $order,
), $options);
}
foreach (array(
'created',
'changed',
) as $date) {
if ($created_tokens = token_find_with_prefix($tokens, $date)) {
$replacements += token_generate('date', $created_tokens, array(
'date' => $order->{$date},
), $options);
}
}
}
return $replacements;
}