-1

I am currently working on a Drupal project that involves a Webform. I need to assign the value of a specific Webform element, particularly the IP address element, to a custom token. The goal is to use this token elsewhere in the site. Could you please provide guidance or steps on how to achieve this integration?

The steps I've taken so far:


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, the IP_ADDRESS always returns NULL. I suspect that the $ip variable is not the correct method for retrieving the value of the Webform Submission IP Address

How to retrieve the Webform Submission IP Address value?

I'll appreciate the help, Thanks

4
  • What does “it didn’t work” mean, technically?
    – mona lisa
    Commented Oct 31, 2022 at 1:30
  • @cilefen oh I forgot to mention that, the $ip return nothing
    – Barry
    Commented Oct 31, 2022 at 1:43
  • How is hook_tokens being passed an object implementing WebformSubmissionInterface? That isn’t part of that hook’s signature.
    – mona lisa
    Commented Oct 31, 2022 at 2:03
  • @cilefen good question, so how to use the 'WebformSubmissionInterface' in token file?
    – Barry
    Commented Oct 31, 2022 at 2:05

2 Answers 2

1

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());
  }
}
1

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!

2
  • hook_tokens() does not get any $webform_submission parameter. The shown code will not work.
    – apaderno
    Commented Feb 24, 2023 at 19:15
  • 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().
    – apaderno
    Commented Mar 26, 2023 at 18:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.