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