【问题标题】:How do I modify a token's value?如何修改令牌的值?
【发布时间】:2012-12-21 07:42:36
【问题描述】:

如何修改令牌的值? (有关详细信息,请参阅下文。)

function hook_webform_submission_insert($node, $submission) {
  // Total_points is a hidden input tag in a web form and initially set as 0.
  // Total points will be calculated here, and assigned to total_points.
  // $total_points = token_replace('[submission:values:total_points]', array("webform-submission" => $submission));

  // How do I modify a token value? e.g.
  $the_token = &drupal_get_token($name_of_token = '[submission:values:total_points]');
  $the_token = "100" // Assign 100 points.
}

  • 在了解了代码流程后,我解决了这个问题。
  • 我尝试做的是替换 webform 中的隐藏变量,然后使用 webform2pdf。
  • 如果您在 webform2pdf 的管理设置中有一些文本。例如[提交:值:total_points]
  • 做 $replacements['[submission:values:total_points]'] = my_value;
  • webform2pdf 将在生成的 pdf 中查看并插入 [submission:values:total_points] 的值(即 my_value)。
  • 我意识到我可以在论坛和谷歌互联网上提问。归根结底,我仍然需要深入研究代码并理解它。

【问题讨论】:

    标签: drupal-7 drupal-webform


    【解决方案1】:

    首先,drupal_get_token() 用于生成防止跨站点请求伪造的值。它通常在创建链接时使用,例如 overlay_disable_message() 所做的。

          'query' => drupal_get_destination() + array(
            // Add a token to protect against cross-site request forgeries.
            'token' => drupal_get_token('overlay'),
          ),
    

    要更改像 [submission:values:total_points] 这样的标记,模块需要实现 hook_tokens_alter()webform_tokens() 使用的代码可以指导您编写应该编写的代码。

    function mymodule_tokens_alter(array &$replacements, array $context) {
      if ($context['type'] == 'submission' && !empty($context['data']['webform-submission'])) {
        // $submission = $context['data']['webform-submission'];
        // $node = $context['data']['node'] ? $context['data']['node'] : node_load($submission->nid);
    
        // Find any token starting with submission:values.
        if ($value_tokens = token_find_with_prefix($context['tokens'], 'values')) {
          if (!empty($value_tokens['total_points'])) {
            $replacements[$value_tokens['total_points']] = 100;
          }
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-11-04
      • 1970-01-01
      • 2022-10-19
      • 1970-01-01
      • 2022-06-14
      • 2017-07-02
      • 2017-07-31
      • 1970-01-01
      • 2019-11-07
      相关资源
      最近更新 更多