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!