You are here

function uc_addresses_token_list in Ubercart Addresses 6.2

Same name and namespace in other branches
  1. 6 uc_addresses.module \uc_addresses_token_list()

Implementation of hook_token_list().

File

./uc_addresses.module, line 1020
Adds user profile address support to Ubercart.

Code

function uc_addresses_token_list($type = 'all') {
  $tokens = array();

  // Ubercart Addresses tokens.
  if ($type == 'uc_addresses' || $type == 'all') {
    $address = UcAddressesAddress::newAddress();
    $fields = uc_addresses_get_address_field_handler_instances($address, 'token');
    foreach ($fields as $fieldname => $handler) {

      // Check if handler supports multiple output formats.
      $formats = $handler
        ->getOutputFormats();
      if (count($formats) > 0) {
        foreach ($formats as $format => $label) {
          $tokens['uc_addresses']['uc_addresses_' . $format] = $label;
        }
      }
      else {

        // Only a single format is supported.
        $tokens['uc_addresses']['uc_addresses_' . $fieldname] = t('!name, formatted', array(
          '!name' => $handler
            ->getFieldTitle(),
        ));
      }
      $tokens['uc_addresses']['uc_addresses_' . $fieldname . '-raw'] = t('!name, raw', array(
        '!name' => $handler
          ->getFieldTitle(),
      ));
    }
  }

  // Ubercart order tokens.
  if ($type == 'order' || $type == 'all') {
    $tokens['order']['uc-addresses-shipping-address'] = t('The order shipping address, formatted with the Ubercart Addresses module.');
    $tokens['order']['uc-addresses-billing-address'] = t('The order billing address, formatted with the Ubercart Addresses module.');
    $address_tokens = uc_addresses_token_list('uc_addresses');
    foreach ($address_tokens['uc_addresses'] as $token => $description) {
      if (strpos($token, 'uc_addresses_default_') === FALSE && strpos($token, 'uc_addresses_aid') === FALSE && strpos($token, 'uc_addresses_uid') === FALSE && strpos($token, 'uc_addresses_address_name') === FALSE && strpos($token, 'uc_addresses_created') === FALSE && strpos($token, 'uc_addresses_modified') === FALSE) {
        $order_token = str_replace('uc_addresses_', 'order-uc_addresses-shipping-', $token);
        $tokens['order'][$order_token] = t('Shipping address') . ': ' . $description;
        $order_token = str_replace('uc_addresses_', 'order-uc_addresses-billing-', $token);
        $tokens['order'][$order_token] = t('Billing address') . ': ' . $description;
      }
    }
  }

  // User tokens.
  if ($type == 'user' || $type == 'all') {
    $address_tokens = uc_addresses_token_list('uc_addresses');
    foreach ($address_tokens['uc_addresses'] as $token => $description) {
      if (strpos($token, 'uc_addresses_default_') === FALSE) {
        $user_token = str_replace('uc_addresses', 'uc_addresses-shipping', $token);
        $tokens[t('Ubercart Addresses') . ' - ' . t('Default shipping address')][$user_token] = t('Default shipping address') . ': ' . $description;
        $user_token = str_replace('uc_addresses', 'uc_addresses-billing', $token);
        $tokens[t('Ubercart Addresses') . ' - ' . t('Default billing address')][$user_token] = t('Default billing address') . ': ' . $description;
      }
      $tokens[t('Ubercart Addresses') . ' - ' . t('Default shipping address')]['uc-addresses-shipping-address'] = t('The default shipping address, formatted with the Ubercart Addresses module.');
      $tokens[t('Ubercart Addresses') . ' - ' . t('Default billing address')]['uc-addresses-billing-address'] = t('The default billing address, formatted with the Ubercart Addresses module.');
    }
  }
  return $tokens;
}