function sms_tokens in SMS Framework 8
Same name and namespace in other branches
- 7 sms.tokens.inc \sms_tokens()
- 2.x sms.tokens.inc \sms_tokens()
- 2.1.x sms.tokens.inc \sms_tokens()
Implements hook_tokens().
File
- ./
sms.tokens.inc, line 46 - Token callbacks for the sms module.
Code
function sms_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
$replacements = [];
if ($type == 'sms') {
foreach ($tokens as $name => $original) {
switch ($name) {
case 'verification-url':
$url_options = [
'absolute' => TRUE,
];
$result = \Drupal::url('sms.phone.verify', [], $url_options, TRUE);
$replacements[$original] = $result
->getGeneratedUrl();
break;
}
}
}
if ($type == 'sms-message') {
if (!empty($data['sms-message']) && $data['sms-message'] instanceof SmsMessageInterface) {
/** @var \Drupal\sms\Message\SmsMessageInterface $sms_message */
$sms_message = $data['sms-message'];
foreach ($tokens as $name => $original) {
switch ($name) {
case 'phone-number':
$recipients = $sms_message
->getRecipients();
$replacements[$original] = reset($recipients) ?: '';
break;
case 'message':
$replacements[$original] = $sms_message
->getMessage();
break;
}
}
}
foreach ($tokens as $name => $original) {
switch ($name) {
case 'verification-code':
if (!empty($data['sms_verification_code'])) {
$replacements[$original] = $data['sms_verification_code'];
}
break;
}
}
}
return $replacements;
}