0

In a custom module i have hook_tokens() implemented in order to create some custom tokens. What i realized is that some data is being declared as null but i don't know where this is happening. These values are coming from user input in a submitted web form. input from traditional web form fields do appear but input added through payment method fields are the ones appearing as null.

this is a small snip of what var_dump($data) shows:

$data['webform-submission']->payments[34]->contextObj->submission->data[47][0]"August"
$data['webform-submission']->payments[34]->contextObj->submission->data[46]array[1]
$data['webform-submission']->payments[34]->contextObj->submission->data[46][0]"1"
$data['webform-submission']->payments[34]->contextObj->submission->data[16]array[1]
$data['webform-submission']->payments[34]->contextObj->submission->data[16][0]null
$data['webform-submission']->payments[34]->contextObj->submission->data[35]array[1]

EDIT:

Thanks for the clarification on where $data comes from. I also realized that the hook i created (hook_token_info) is not being utilized. When i create a token i do not see it listed under Browse available tokens when i edit emails templates under the web form tab. Oddly hook_tokens is being called when i pass token_alter('[submission:custom:card_type]', array('submission'=>$card_type)) however the token is empty.

1

The $data is whatever is originally passed to the token_replace function by the process that's doing the token replacement.

The var itself is:

An array of keyed objects. For simple replacement scenarios 'node', 'user', and others are common keys, with an accompanying node or user object being the value. Some token types, like 'site', do not require any explicit information from $data and can be replaced even if it is empty.

It's essentially just an array that's used to pass extra context to the token replacement, and it can contain anything at all.

For example if you were writing a module that introduced a custom entity type, you could use this feature to pass an instance of that type in to be used in your own tokens.

In your specific case, it's presumably the Webform module that prepares that data array, so that would be where to look if something's missing. Or it could be a submodule if that's what you've used to provide the payment fields themselves.

  • does this mean that when i make a call using: token_replace($text, array $data = array(), array $options = array()) $text is the name of the token and $data is an array containing the token type and its new value? – blu Nov 16 '16 at 20:36
  • No, when you call token_replace all implementations of hook_tokens defined by any installed module are invoked, after Drupal has extracted the token types from the incoming text, and each of those hook functions can update the replacement for any of the types it might be interested in. $data provides extra context for that decision – Clive Nov 16 '16 at 23:47

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.