You are here

function commerce_cardonfile_tokens in Commerce Card on File 7.2

Implements hook_tokens().

File

./commerce_cardonfile.tokens.inc, line 49
Provides token integration.

Code

function commerce_cardonfile_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $token_info = array(
    'commerce-cardonfile-charge-card-response' => array(
      'property_type' => 'commerce_cardonfile_charge_card_response',
      'property_callback' => 'commerce_cardonfile_charge_card_response_property_info_callback',
    ),
  );

  // Wrong type or no data provided.
  if (empty($token_info[$type]) || empty($data[$type])) {
    return array();
  }

  // Make a pseudo wrapper for response data.
  $wrapper_info = array();
  if (isset($token_info[$type]['property_callback'])) {
    $property_info = $token_info[$type]['property_callback']();
    $wrapper_info = array(
      'property info' => $property_info,
    );
  }
  $wrapper = new EntityStructureWrapper($token_info[$type]['property_type'], $data[$type], $wrapper_info);
  $sanitize = !empty($options['sanitize']);
  $replacements = array();
  foreach ($tokens as $name => $original) {
    $replacement_value = NULL;
    $property_name = str_replace('-', '_', $name);
    if (isset($wrapper->{$property_name})) {
      $property_value = $wrapper->{$property_name}
        ->value();
      if (!is_object($property_value)) {
        $replacement_value = $property_value;
      }
    }
    if (isset($replacement_value)) {
      $replacements[$original] = $sanitize ? check_plain($replacement_value) : $replacement_value;
    }
  }
  return $replacements;
}