0

I am trying to create custom tokens, but my implementation doesn't work. I have implemented hook_token_info() and hook_tokens() like this in my .theme file for Drupal 7.

/**
* Implements hook_token_info().
*/
function THEMENAME_token_info() {
  $info = array();

  $info['types'] = array(
    'commerce-order:commerce-customer-billing' => array(
      'name' => t('Commerce Order Billing'),
      'description' => t('Global commerce order billing tokens'),
     ),
  );
  $info['tokens'] = array(
    'commerce-order:commerce-customer-billing' => array(
      'address-info' => array(
        'name' => t('Commerce address information'),
        'description' => t('Token for commerce address information'),
      ),
    ),
  );
  return $info;
}

/**
*
* Implements hook_tokens().
*/
function THEMENAME_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $replacements = array();

  if ($type == 'commerce-order:commerce-customer-billing') {
    foreach ($tokens as $name => $original) {
      switch ($name) {
      case 'address-info':
        $address_info = theme('nr_commerce_address_information', array('order' => $order));
        $replacements[$original] = $address_info;
        break;
      }
    }
  }
}

When I see the list of available tokens in the interface, my tokens does not show up. Why? Is there anything wrong in my code, or is the reason another one?

  • I assume you have cleared your cache? Also, are you sure that noreba module is enabled? Also, D7 or D8? – bleen Jul 18 '17 at 14:56
2

I found out. It was because I tried to implement hooks in my template rather than doing it in my module.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.