You are here

function commerce_customer_tokens in Commerce Core 7

Implements hook_tokens().

File

modules/customer/commerce_customer.tokens.inc, line 64
Builds placeholder replacement tokens for customer related data.

Code

function commerce_customer_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $url_options = array(
    'absolute' => TRUE,
  );
  if (isset($options['language'])) {
    $url_options['language'] = $options['language'];
    $language_code = $options['language']->language;
  }
  else {
    $language_code = NULL;
  }
  $sanitize = !empty($options['sanitize']);
  $replacements = array();
  if ($type == 'commerce-customer-profile' && !empty($data['commerce-customer-profile'])) {
    $profile = $data['commerce-customer-profile'];
    foreach ($tokens as $name => $original) {
      switch ($name) {

        // Simple key values on the customer profile.
        case 'customer-profile-id':
          $replacements[$original] = $profile->profile_id;
          break;
        case 'revision-id':
          $replacements[$original] = $profile->revision_id;
          break;
        case 'type':
          $replacements[$original] = $sanitize ? check_plain($profile->type) : $profile->type;
          break;
        case 'type-name':
          $replacements[$original] = commerce_customer_profile_type_get_name($profile->type);
          break;

        // Default values for the chained tokens handled below.
        case 'owner':
          if ($profile->uid == 0) {
            $name = variable_get('anonymous', t('Anonymous'));
          }
          else {
            $account = user_load($profile->uid);
            $name = $account->name;
          }
          $replacements[$original] = $sanitize ? filter_xss($name) : $name;
          break;
        case 'created':
          $replacements[$original] = format_date($profile->created, 'medium', '', NULL, $language_code);
          break;
        case 'changed':
          $replacements[$original] = format_date($profile->changed, 'medium', '', NULL, $language_code);
          break;
      }
    }
    if ($owner_tokens = token_find_with_prefix($tokens, 'owner')) {
      $owner = user_load($profile->uid);
      $replacements += token_generate('user', $owner_tokens, array(
        'user' => $owner,
      ), $options);
    }
    foreach (array(
      'created',
      'changed',
    ) as $date) {
      if ($created_tokens = token_find_with_prefix($tokens, $date)) {
        $replacements += token_generate('date', $created_tokens, array(
          'date' => $profile->{$date},
        ), $options);
      }
    }
  }
  return $replacements;
}