You are here

function commerce_cop_tokens in Commerce Custom Offline Payments 7

Implements hook_tokens().

File

./commerce_cop.module, line 468
Custom offline payment methods for Drupal Commerce.

Code

function commerce_cop_tokens($type, $tokens, array $data = array(), array $options = array()) {
  static $token_replacement_running;
  $replacements = array();
  if ($type == 'commerce-order' && !empty($data['commerce-order'])) {
    $order = $data['commerce-order'];

    // Get the order payment method if it is set.
    if (isset($order->data['payment_method'])) {
      $payment_method = commerce_payment_method_instance_load($order->data['payment_method']);

      // Use the payment method information settings if exists.
      if (!empty($payment_method['settings']['information'])) {
        $payment_info = check_markup($payment_method['settings']['information']['value'], $payment_method['settings']['information']['format']);
      }
      else {
        $payment = custom_offline_payment_load($payment_method['method_id']);
        if ($payment && !empty($payment['information'])) {
          $payment_info = $payment['information'];

          // Run token replacement.
          if (!isset($token_replacement_running)) {

            // We need to avoid recursion since commerce_cop_tokens() calls this
            // function too. So if a token replacement is running don't trigger another
            // token replacement.
            $token_replacement_running = TRUE;
            $payment_info = token_replace($payment_info, array(
              'commerce_order' => $order,
            ), array(
              'language' => $GLOBALS['language'],
              'clear' => TRUE,
            ));
            unset($token_replacement_running);
          }
        }
      }
      if (!empty($payment_info)) {
        foreach ($tokens as $name => $original) {
          switch ($name) {
            case 'payment-method-information':
              $replacements[$original] = $payment_info;
              break;
          }
        }
      }
    }
  }
  return $replacements;
}