I have Drupal Commerce, and it sends confirmation emails after product is sold. In those emails are tokens. I have token [commerce-order:created]
and I'm interested if is there any conveniant way to change a formatting of that date.
2 Answers
If [commerce-order:created]
is a date token, then you can use [commerce-order:created:custom:<the format you want to apply>]
, where <the format you want to apply>
is the string format accepted by date()
.
Otherwise, the more generic way to alter tokens implemented by other modules is implementing hook_tokens_alter()
.
function mymodule_tokens_alter(&$replacements, $context) {
$options = $context['options'];
$sanitize = !empty($options['sanitize']);
$langcode = !empty($options['language']->language) ? $options['language']->language : NULL;
if ($context['type'] == 'commerce-order') {
foreach ($context['tokens'] as $name => $original) {
switch ($name) {
case 'created':
// Set a value for $sanitized and $new_value.
$replacements[$original] = $sanitize ? $sanitized : $new_value;
}
}
}
}
Notice that in my code:
$sanitized
is the sanitized value to assign to the token, used only when$sanitize
isTRUE
$new_value
is the new value for the token, used to obtain the value of$sanitized
You can do this with hook_tokens_alter
Alter replacement values for placeholder tokens.
Eg:
/**
* Implements hook_tokens_alter().
*/
function mymodule_tokens_alter(&$replacements, $context){
// May be you can do some more ifs to reduce the load :)
if(isset($replacements['[commerce-order:created]'])){
$replacements['[commerce-order:created]'] = check_plain('YOUR-CUSTOM-REPLACEMENT');
}
}