3

I created the following token; however, when I try to use site:coupons as a data selector in a loop action It does not appear in data selection browser. Note that it does appear as replacement pattern when i use for example "Show a message on the site" action.

I spent lot of time searching in the internet and rules' token' issue queue, i tried to read the source codes of core token , token and rules as well. I also found some information too like data selector are no tokens! or rules only works with entities! So far i couldn't get this to work no matter hard i tried. My data is not entity. Is there anyway to integrate it with rules? I couldn't find any official documentation on this so i created an issue with hope that some of the rule's experts can help me out.

Note : if i replace site with coupon-link in the following code, it won't even appear as replacement pattern in rules. but it works fine as token anywhere else but in rules

Thanks in advance

<?php
/**
* Implements hook_token_info().
*/
function coupon_link_token_info() {
$types['coupon-link'] = array(
'name' => t("Coupon link coupon info"),
'description' => t("Info about linked coupon via url."),
);

// Andy Pangus specific tokens.
$tokens['site']['coupon-code'] = array(
'name' => t("Coupon Link Coupon Code"),
'description' => t("The code of the coupon entered via url."),
);
$tokens['site']['coupon'] = array(
'name' => t("Coupon Link Coupon"),
'description' => t("The coupon entered via url."),
'type' => 'commerce_coupon'
);
$tokens['site']['coupons'] = array(
'name' => t("Coupon Link List Coupons"),
'description' => t("The coupons entered via url."),
'type' => 'array'
);

return array(
'types' => $types,
'tokens' => $tokens,
);
}

/**
* Implements hook_tokens().
*
* @ingroup token_example
*/
function coupon_link_tokens($type, $tokens, array $data = array(), array $options =         array()) {
$replacements = array();
$sanitize = !empty($options['sanitize']);

// Text format tokens.
if ($type == 'site' && __coupon_link_get_coupon_code()) {
//$format = $data['format'];

foreach ($tokens as $name => $original) {
switch ($name) {
case 'coupon-code':
// Since {filter_format}.format is an integer and not user-entered
// text, it does not need to ever be sanitized.
$replacements[$original] = $sanitize ? filter_xss(__coupon_link_get_coupon_code()) :     __coupon_link_get_coupon_code();
break;
case 'coupon':
// Since the format name is user-entered text, santize when requested.
$replacements[$original] = __coupon_link_get_coupon(__coupon_link_get_coupon_code());
break;
case 'coupons':
// Since the format name is user-entered text, santize when requested.
$replacements[$original] =                 array(__coupon_link_get_coupon(__coupon_link_get_coupon_code()));
break;
}
}
}
return $replacements;
}
?>
apaderno
  • 28,547
  • 16
  • 75
  • 90
Sina Salek
  • 323
  • 2
  • 14

1 Answers1

1

A few things.

  1. Tokens are formatted as [type:token] as explained on the hook_token_info api page. For your example, it would be [coupon-link:coupon]. I'm not sure why you're appending your tokens to the site array, as your custom coupon token probably has nothing to do with sitewide tokens like *site_url* or *site_name*.

  2. Because the types are machine names, you should change it to coupon_link as machine names with dashes are not Drupal standard.

  3. If you truly get lost, I suggest also looking at the token example from the examples module.

fmitchell
  • 891
  • 1
  • 6
  • 7