You are here

function commerce_bpc_tokens in Commerce Bulk Product Creation 7

Same name and namespace in other branches
  1. 7.2 commerce_bpc.tokens.inc \commerce_bpc_tokens()

Implements hook_tokens().

File

./commerce_bpc.tokens.inc, line 60
Token API implementations for Commerce bulk product creation module.

Code

function commerce_bpc_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $sanitize = !empty($options['sanitize']);
  $replacements = array();
  if ($type == 'bulk_product' && !empty($data['bulk_data'])) {
    $bulk_data = $data['bulk_data'];
    $field_vals = commerce_bpc_tokens_by_product_type($bulk_data['product_type'], $bulk_data['combination'], $options);
    foreach ($field_vals['values'] as $field_name => $value) {
      if (isset($tokens[$field_name . '-value'])) {
        $replacements[$tokens[$field_name . '-value']] = $value;
      }
    }
    foreach ($field_vals['labels'] as $field_name => $label) {
      if (isset($tokens[$field_name . '-label'])) {
        $replacements[$tokens[$field_name . '-label']] = $label;
      }
    }
  }
  if ($type == 'bulk_defaults' && !empty($data['bulk_data'])) {
    $product_type = $data['bulk_data']['product_type'];

    // Get the names of all tokens for the current product type.
    if (isset($data['bulk_data']['combination'])) {

      // Collect tokens.
      $field_replacements = commerce_bpc_tokens_by_product_type($product_type, $data['bulk_data']['combination'], $options);
      if (!empty($field_replacements)) {
        if (!empty($tokens['combination_values'])) {
          $values = $field_replacements['values'];
          $separator = commerce_bpc_setting('default', 'sku_separator', $product_type);
          $replacements[$tokens['combination_values']] = implode($separator, $values);
        }
        if (!empty($tokens['combination_labels'])) {
          $labels = $field_replacements['labels'];
          $separator = commerce_bpc_setting('default', 'title_separator', $product_type);
          $replacements[$tokens['combination_labels']] = implode($separator, $labels);
        }
      }
    }
    if (!empty($data['bulk_data']['sku_fragment']) && !empty($tokens['entered_sku'])) {
      $replacements[$tokens['entered_sku']] = $data['bulk_data']['sku_fragment'];
    }
    if (!empty($data['bulk_data']['title_fragment']) && !empty($tokens['entered_title'])) {
      $replacements[$tokens['entered_title']] = $data['bulk_data']['title_fragment'];
    }
  }
  return $replacements;
}