You are here

function bpc_taxonomy_commerce_bpc_tokens in Commerce Bulk Product Creation 7

Same name and namespace in other branches
  1. 7.2 modules/bpc_taxonomy/bpc_taxonomy.module \bpc_taxonomy_commerce_bpc_tokens()

Implements hook_commerce_bpc_tokens().

File

modules/bpc_taxonomy/bpc_taxonomy.module, line 115
The Commerce BPC Taxonomy integration module enables the use of taxonomy term references in bulk creation.

Code

function bpc_taxonomy_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['type'] == 'taxonomy_term_reference' && !_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]['tid'];
      $replacements['values'][$field_name] = $value;
      $labels = taxonomy_allowed_values($field);
      if (!empty($value)) {

        // $value can be empty in the case of sample value generation for
        // an empty vocabulary.
        $replacements['labels'][$field_name] = $sanitize ? check_plain($labels[$value]) : $labels[$value];
      }
      else {

        // If empty, we just use the dummy term "example"
        $replacements['labels'][$field_name] = "example";
      }
    }
  }
  return $replacements;
}