I am using following code to replace current-user:email token.

 $pattern = '[current-user:mail]';
 $output = $token_service->replace($pattern, array('current-user' => $account), array(
            'clear' => TRUE,
            'callback' => xyz_function($pattern_array),

In the callback function i am doing

foreach ($replacements as $token => $value) {
  $replacements[$token] = _cleanstring($value);

I guess i am passing wrong array('current-user' => $account) argument. Where am i doing wrong?

  • Are you sure that it's not your callback function that's doing something wrong? Does it work without that? And what happens if you remove clear? Just for testing.. Is that really your code, because $pattern isn't a string, doing that would result in an array with some php notices (make sure to have error display set to verbose, helps to spot errors)
    – Berdir
    Jan 5 '17 at 20:40
  • I have updated the $pattern string in my Question. I am using right in my code it is just that i missed string here. Yes, there is quite lot of chance that my function callback might be wrong. But when i use $output = $token_service->replace($pattern); in that case as well i receive $output as null. Also inside the function callback ,inside foreach loop i recive $value as token string , which i think $value should token string replacement value like Drupal 7.
    – neha
    Jan 6 '17 at 7:19
  • As commented by 4k4, current-user does not use input. It uses the current user. Are you sure that you actually have one in your context? Try \Drupal::currentUser()->getEmail(). Drush for example defaults to the anon user which does not have an e-mail, but this works fine when I use drush --user=1 core-cli: \Drupal::token()->replace('foo [current-user:mail]');
    – Berdir
    Jan 6 '17 at 7:45
  • I am calling entire code inside hook_user_insert($account). \Drupal::currentUser()->id() and \Drupal::currentUser()->getEmail() are empty inside this . Although i get proper $account->id() value. I have tried a) $output = $token_service->replace($pattern);, b) $output = $token_service->replace($pattern, array(current-user=> $account)); c) $token_service->replace($pattern, array(user=> $account)); and many other combinations. Nothing works.
    – neha
    Jan 6 '17 at 8:40
  • 1
    Yes, as 4k4 said from the start, you are using the wrong tokens. current-user is the current user, not the one that is currently being saved. Sounds like this might be registration, where the current user is still anon. You need to use user:mail as token.
    – Berdir
    Jan 6 '17 at 9:47

Tokens for the current user [current-user:*] don't need any data. Leave the array empty.

What you might referring to are tokens for any user [user:*]. They need an account as parameter 'user'.

  • 1
    I also tried $output = $token_service->replace($pattern) and many combination, $output is coming blank.
    – neha
    Jan 5 '17 at 20:02

Instead of token replace use -> normal placeholder in t function D8

eg: $body = t("@username is my Name.", ['@username' => "Jyoti"]);

Output will be "Jyoti is my Name."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.