【问题标题】:Using token as data selector使用令牌作为数据选择器
【发布时间】:2012-10-15 19:52:23
【问题描述】:

我创建了以下令牌;但是,当我尝试在循环操作中使用 site:coupons 作为数据选择器时 它不会出现在数据选择浏览器中。请注意,当我使用例如“在网站上显示消息”操作时,它确实显示为替换模式。

我花了很多时间在互联网和规则'令牌'问题队列中搜索,我试图阅读核心令牌,令牌和规则的源代码。我还发现了一些信息,比如数据选择器没有标记!或规则仅适用于实体! 到目前为止,无论我多么努力,我都无法让它发挥作用。我的数据不是实体。反正有没有将它与规则相结合? 我找不到任何官方文档,所以我创建了一个问题,希望一些规则专家可以帮助我。

注意:如果我在下面的代码中用优惠券链接替换网站,它甚至不会在规则中显示为替换模式。但它在其他任何地方都可以作为令牌正常工作,但在规则中

提前致谢

<?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;
}
?>

【问题讨论】:

    标签: drupal drupal-7 token drupal-rules


    【解决方案1】:

    一些事情。

    1. 令牌格式为[type:token],如the hook_token_info api 页面所述。对于您的示例,它将是[coupon-link:coupon]。我不确定您为什么要将令牌附加到 site 数组,因为您的自定义优惠券令牌可能与 *site_url* 或 *site_name* 等站点范围的令牌无关。

    2. 因为类型是机器名,你应该将其更改为coupon_link,因为带有破折号的机器名不是 Drupal 标准。

    3. 如果你真的迷路了,我建议你也看看示例模块中的the token example

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      • 2014-09-24
      • 2017-08-13
      • 2013-06-09
      • 2018-04-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多