Score:-1

How to get Webform Element value into Custom Token

za flag

How to assign a value of Webform Element (IP_ADDRESS) to a Custom Token ?

The Step that I did:


use Drupal\webform\WebformSubmissionInterface;

function MODULE_token_info() {
// token info
}

function MODULE_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata, WebformSubmissionInterface $webform_submission = NULL) {
  
  // @var to return IP address of the submission.
 
  $ip = $webform_submission->getRemoteAddr();

  $replacements = [];
  if ($type == 'custom') {
    foreach ($tokens as $name => $original) {
      switch ($name) {

        case 'order-cart':
          $replacements[$original] = t('First Value');
          break;

        case 'city-token':
          $replacements[$original] = getCity($ip);
          break;

      }
    }
  }

  return $replacements;
}

However, IP_ADDRESS always return NULL. I'm sure the $ip var is not the right way to retrieve Element value. and that's the issue.

So, How to return the value of Webform Submission IP Address? `

I'll appreciate the help, Thanks

id flag
What does “it didn’t work” mean, technically?
a.alshamiri avatar
za flag
@cilefen oh I forgot to mention that, the $ip return nothing
id flag
How is hook_tokens being passed an object implementing WebformSubmissionInterface? That isn’t part of that hook’s signature.
a.alshamiri avatar
za flag
@cilefen good question, so how to use the 'WebformSubmissionInterface' in token file?
Score:1
cn flag

This is updated version cause of your comment below.

To get a webform element value into a custom token, you need to first create a custom token and then use the hook_tokens function to assign a value to that token.

To do this, you can follow these steps:

Define your custom token using the hook_token_info() function. This function defines the name and description of your custom token.

function MODULE_token_info() {
  $info['tokens']['city-token'] = [
    'name' => t('City Token'),
    'description' => t('Replaces [city-token] with the city name.'),
  ];
  return $info;
}

Implement hook_tokens function to replace the custom token with the desired value. This function takes several parameters, including the type of tokens being requested, the tokens themselves, the data used to replace the tokens, and the webform submission object.

function MODULE_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
  $replacements = array();
  if ($type == 'custom') {
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'city-token':
          // Get the IP address element object from the submission.
          $webform_submission = $data['webform_submission'];
          $ip_element = $webform_submission->getElementData('ip_address');
          if ($ip_element) {
            // Get the value of the IP address element.
            $ip_address = $ip_element->getValue();
            $replacements[$original] = getCity($ip_address);
          }
          break;
      }
    }
  }
  return $replacements;
}

Note that the $webform_submission parameter is passed in as part of the $data array. This allows you to access the webform submission object and retrieve the value of the desired element.

With these functions implemented, you should be able to use the [city-token] token in any text field within your webform and it will be replaced with the value of the city corresponding to the IP address provided in the IP_ADDRESS element.

I hope this help!

apaderno avatar
us flag
[`hook_tokens()`](https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Utility%21token.api.php/function/hook_tokens/9) does not get any `$webform_submission` parameter. The shown code will not work.
apaderno avatar
us flag
Still, `MODULE_tokens()` will not get any `$data['webform_submission']` if `MODULE_token_info()` does not add `'needs-data' => 'webform_submission'` to the array it returns. The answer is still wrong, as to add tokens to the ones returned by a module, it is necessary to implement `hook_token_alter()`.
Score:0
us flag

The only tokens with access to a webform submission are the webform_submission tokens defined in webform_token_info(). You can add new tokens implementing hook_token_info_alter() (for the token description) and hook_tokens_alter() (for the token value).

The relevant parts in hook_tokens_alter() are:

  • Verify $context['type'] is equal to 'webform_submission'
  • Verify $context['data']['webform_submission'] is set
use Drupal\Core\Render\BubbleableMetadata;

function mymodule_tokens_alter(array &$replacements, array $context, BubbleableMetadata $bubbleable_metadata) {
  if ($context['type'] == 'webform_submission' && !empty($context['data']['webform_submission'])) {
    $webform_submission = $context['data']['webform_submission'];
    // Replace 'city' with the token name.
    $replacements[$context['tokens']['city']] = mymodule_get_city($webform_submission->getRemoteAddr());
  }
}
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.