function hook_commerce_bpc_tokens in Commerce Bulk Product Creation 7
Same name and namespace in other branches
- 7.2 commerce_bpc.api.php \hook_commerce_bpc_tokens()
Specify token replacement values for combinations.
Combination-providing modules should implement this hook.
commerce_bpc handles token management for combinations, to ensure a minimally consistent set of tokens. In particular, commerce_bpc will create a FIELD_NAME-value and a FIELD_NAME-label token for each combination-creating field. The -value token should be fit for inclusion in SKUs, while the -label token should be a human readable value.
Parameters
string $product_type: The product type to provide tokens for.
array $combination: An array with field names as keys and their respective values as values.
array $options: Options array passed unmodified from hook_tokens(). Make sure you honor the 'sanitize' property.
Return value
array An array with two keys 'values' and 'labels', each of which should have an array keyed by field names with replacements as values.
See also
hook_commerce_bpc_get_combinations()
2 functions implement hook_commerce_bpc_tokens()
Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.
- bpc_taxonomy_commerce_bpc_tokens in modules/
bpc_taxonomy/ bpc_taxonomy.module - Implements hook_commerce_bpc_tokens().
- commerce_bpc_commerce_bpc_tokens in ./
commerce_bpc.hooks_list.inc - Implements hook_commerce_bpc_tokens().
1 invocation of hook_commerce_bpc_tokens()
- commerce_bpc_tokens_by_product_type in ./
commerce_bpc.tokens.inc - Collects field-specific token replacement for a given product type.
File
- ./
commerce_bpc.api.php, line 302 - This file contains no working PHP code; it exists to provide additional documentation for doxygen as well as to document hooks in the standard Drupal manner.
Code
function hook_commerce_bpc_tokens($product_type, $combination, $options) {
$replacements = array();
$sanitize = !empty($options['sanitize']);
foreach ($combination as $field_name => $values) {
$field = field_info_field($field_name);
$instance = field_info_instance('commerce_product', $field_name, $product_type);
if ($field['module'] == 'list' && !_commerce_bpc_get_value($instance, array(
'commerce_bpc',
'is_static',
), FALSE)) {
// TODO: Treat languages properly.
$items = reset($values);
// We only allow a single value per combination, so we can treat
// this like a single value field.
$value = $items[0]['value'];
$replacements['values'][$field_name] = $value;
$labels = list_allowed_values($field);
$replacements['labels'][$field_name] = $sanitize ? check_plain($labels[$value]) : $labels[$value];
}
}
return $replacements;
}