2012-12-21 27 views
0

如何修改令牌的值? (請參見下面的更多信息。)如何修改令牌的值?

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的管理設置中有一些文字。例如[submission:values:total_points]
  • Do $ replacements ['[submission:values:total_points]'] = my_value;
  • webform2pdf將在生成的pdf中看到並插入[submission:values:total_points](即my_value)的值。
  • 我意識到我可以在論壇和谷歌互聯網問。在一天結束時,我仍然需要深入瞭解代碼並理解它。

回答

3

首先,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'), 
    ), 

爲了改變像令牌[提交內容:值: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; 
     } 
    } 
    } 
}