1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Builds placeholder replacement tokens for product-related data.
|
6
|
*/
|
7
|
|
8
|
|
9
|
/**
|
10
|
* Implements hook_token_info().
|
11
|
*/
|
12
|
function commerce_product_token_info() {
|
13
|
$type = array(
|
14
|
'name' => t('Products'),
|
15
|
'description' => t('Tokens related to individual products.'),
|
16
|
'needs-data' => 'commerce-product',
|
17
|
);
|
18
|
|
19
|
// Tokens for products.
|
20
|
$product = array();
|
21
|
|
22
|
$product['product-id'] = array(
|
23
|
'name' => t('Product ID'),
|
24
|
'description' => t('The internal numeric ID of the product.'),
|
25
|
);
|
26
|
$product['sku'] = array(
|
27
|
'name' => t('SKU'),
|
28
|
'description' => t('The human readable product SKU.'),
|
29
|
);
|
30
|
$product['type'] = array(
|
31
|
'name' => t('Type'),
|
32
|
'description' => t('The machine name of the product type.'),
|
33
|
);
|
34
|
$product['type-name'] = array(
|
35
|
'name' => t('Type name'),
|
36
|
'description' => t('The human readable name of the product type.'),
|
37
|
);
|
38
|
$product['title'] = array(
|
39
|
'name' => t('Title'),
|
40
|
'description' => t('The title of the product.'),
|
41
|
);
|
42
|
|
43
|
// Chained tokens for products.
|
44
|
$product['creator'] = array(
|
45
|
'name' => t('Creator'),
|
46
|
'description' => t('The creator of the product.'),
|
47
|
'type' => 'user',
|
48
|
);
|
49
|
$product['created'] = array(
|
50
|
'name' => t('Date created'),
|
51
|
'description' => t('The date the product was created.'),
|
52
|
'type' => 'date',
|
53
|
);
|
54
|
$product['changed'] = array(
|
55
|
'name' => t('Date updated'),
|
56
|
'description' => t('The date the product was last updated.'),
|
57
|
'type' => 'date',
|
58
|
);
|
59
|
|
60
|
return array(
|
61
|
'types' => array('commerce-product' => $type),
|
62
|
'tokens' => array('commerce-product' => $product),
|
63
|
);
|
64
|
}
|
65
|
|
66
|
/**
|
67
|
* Implements hook_tokens().
|
68
|
*/
|
69
|
function commerce_product_tokens($type, $tokens, array $data = array(), array $options = array()) {
|
70
|
$url_options = array('absolute' => TRUE);
|
71
|
|
72
|
if (isset($options['language'])) {
|
73
|
$url_options['language'] = $options['language'];
|
74
|
$language_code = $options['language']->language;
|
75
|
}
|
76
|
else {
|
77
|
$language_code = NULL;
|
78
|
}
|
79
|
|
80
|
$sanitize = !empty($options['sanitize']);
|
81
|
|
82
|
$replacements = array();
|
83
|
|
84
|
if ($type == 'commerce-product' && !empty($data['commerce-product'])) {
|
85
|
$product = $data['commerce-product'];
|
86
|
|
87
|
foreach ($tokens as $name => $original) {
|
88
|
switch ($name) {
|
89
|
// Simple key values on the product.
|
90
|
case 'product-id':
|
91
|
$replacements[$original] = $product->product_id;
|
92
|
break;
|
93
|
|
94
|
case 'sku':
|
95
|
$replacements[$original] = $sanitize ? check_plain($product->sku) : $product->sku;
|
96
|
break;
|
97
|
|
98
|
case 'type':
|
99
|
$replacements[$original] = $sanitize ? check_plain($product->type) : $product->type;
|
100
|
break;
|
101
|
|
102
|
case 'type-name':
|
103
|
$replacements[$original] = $sanitize ? check_plain(commerce_product_type_get_name($product->type)) : commerce_product_type_get_name($product->type);
|
104
|
break;
|
105
|
|
106
|
case 'title':
|
107
|
$replacements[$original] = $sanitize ? check_plain($product->title) : $product->title;
|
108
|
break;
|
109
|
|
110
|
// Default values for the chained tokens handled below.
|
111
|
case 'creator':
|
112
|
if (!$product->uid) {
|
113
|
$name = variable_get('anonymous', t('Anonymous'));
|
114
|
}
|
115
|
else {
|
116
|
$creator = user_load($product->uid);
|
117
|
$name = $creator->name;
|
118
|
}
|
119
|
$replacements[$original] = $sanitize ? filter_xss($name) : $name;
|
120
|
break;
|
121
|
case 'created':
|
122
|
$replacements[$original] = format_date($product->created, 'medium', '', NULL, $language_code);
|
123
|
break;
|
124
|
|
125
|
case 'changed':
|
126
|
$replacements[$original] = format_date($product->changed, 'medium', '', NULL, $language_code);
|
127
|
break;
|
128
|
}
|
129
|
}
|
130
|
|
131
|
if ($creator_tokens = token_find_with_prefix($tokens, 'creator')) {
|
132
|
$creator = user_load($product->uid);
|
133
|
$replacements += token_generate('user', $creator_tokens, array('user' => $creator), $options);
|
134
|
}
|
135
|
|
136
|
foreach (array('created', 'changed') as $date) {
|
137
|
if ($created_tokens = token_find_with_prefix($tokens, $date)) {
|
138
|
$replacements += token_generate('date', $created_tokens, array('date' => $product->{$date}), $options);
|
139
|
}
|
140
|
}
|
141
|
}
|
142
|
|
143
|
return $replacements;
|
144
|
}
|