hook_tokens_alter: alter existing core token value

Created on 8 October 2015, over 9 years ago
Updated 15 April 2025, 11 days ago

hook_tokens_alter documentation says "Alter replacement values for placeholder tokens." I would like to alter values for existing core user tokens (I have encrypted core user data that needs to be decrypted). Here is my hook_tokens_alter (taken basically from core "user.tokens.inc"):

/**
 * Implements hook_tokens_alter().
 */
function my_module_tokens_alter(&$replacements,  $context){
    if(isset($context['data']['entity'])) {
        $url_options = array('absolute' => TRUE);
        if (isset($options['language'])) {
            $url_options['language'] = $context['options']['language'];
            $language_code = $context['options']['language']->language;
        } else {
            $language_code = NULL;
        }
        $sanitize = !empty($context['options']['sanitize']);

        if ($context['data']['entity_type'] == 'user' && !empty($context['data']['entity'])) {
            $account = $context['data']['entity'];
            foreach ($context['tokens'] as $name => $original) {
                switch ($name) {
                    // Basic user account information.
                    case 'name':
                        $account->name = my_module_get_decrypted_user_field($account, 'name');
                        $name = format_username($account);
                        $replacements[$original] = $sanitize ? check_plain($name) : $name;
                        break;
                    case 'mail':
                        $mail = my_module_get_decrypted_user_field($account, 'mail');
                        $replacements[$original] = $sanitize ? check_plain($mail) : $mail;
                        break;
                }
            }
        }
    }
}

When I use my module, I get errors like this:

Warning: Illegal offset type in isset or empty in pathauto_cleanstring() (line 180 of /var/www/vhosts/drupal/sites/all/modules/contrib/pathauto/pathauto.inc).
Warning: html_entity_decode() expects parameter 1 to be string, array given in decode_entities() (line 459 of /var/www/vhosts/drupal/includes/unicode.inc).
Warning: Illegal offset type in pathauto_cleanstring() (line 223 of /var/www/vhosts/drupal/sites/all/modules/contrib/pathauto/pathauto.inc).
Notice: Array to string conversion in RulesTokenEvaluator->evaluate() (line 220 of /var/www/vhosts/drupal/sites/all/modules/contrib/rules/modules/system.eval.inc).
Notice: Array to string conversion in RulesTokenEvaluator->evaluate() (line 220 of /var/www/vhosts/drupal/sites/all/modules/contrib/rules/modules/system.eval.inc).
Notice: Array to string conversion in RulesTokenEvaluator->evaluate() (line 220 of /var/www/vhosts/drupal/sites/all/modules/contrib/rules/modules/system.eval.inc).
Notice: Array to string conversion in token_replace() (line 102 of /var/www/vhosts/drupal/includes/token.inc).
Notice: Array to string conversion in token_replace() (line 102 of /var/www/vhosts/drupal/includes/token.inc).
Notice: Array to string conversion in token_replace() (line 102 of /var/www/vhosts/drupal/includes/token.inc).
Notice: Array to string conversion in token_replace() (line 102 of /var/www/vhosts/drupal/includes/token.inc).
Notice: Array to string conversion in token_replace() (line 102 of /var/www/vhosts/drupal/includes/token.inc).

Googling this suggests I cannot actually alter existing token values? E.g.:

https://www.drupal.org/node/1090596 β†’
https://www.drupal.org/node/1649458 β†’

πŸ’¬ Support request
Status

Active

Version

7.0 ⚰️

Component

token system

Created by

πŸ‡ΊπŸ‡ΈUnited States jduhls

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024