I have created a form by extending FormBase class. In form, I'm having a field called "name" where user can input his name. While submitting form, I want that submitted name convert into token so that I can use that token sending emails.

example.com/admin/config/people/accounts, here is the Drupal default configuration email forms.

What I want is to use custom form field value to be appear as a token inside drupal default email config forms.How can this be achieved?

Any help is appreciated. Thanks.

1 Answer 1


Tokens are just a representation of some other data structure that exists.

You can not convert an arbitrary value into a token. What you need to do is store the value and then define a token for that stored value using hook_token_info()/hook_tokens().

I understand it's a per-user value, so the simplest option seems to define a field on the user entity, I don't quite understand why you don't just show that in the profile. Then you can easily access it as a token when using the token module.

  • I'm sending an email invite to any user. Now, default drupal email forms take username as token, and since username is unique, I have created a new field in user entity and want to replace [display name token] with my new field. suppose if I'm admin and sending invite to xyz user, that xyz value should appear as token in email body form. This xyz is my new field defined in user entity. In hook_token, I have loaded current user, and get display name, but I want the display name of invited user created by admin. Sep 4, 2018 at 7:10
  • @SugandhKhanna Your use case is not clear. Are you sending these emails using a custom module? Or you're just using Drupal's default welcome emails' templates? Sep 4, 2018 at 7:59
  • Drupal's default welcome emails' templates Sep 4, 2018 at 8:59
  • Well, then if you're using Token module, you can use the token browser to get that field value and add it to the email template; or this field isn't showing? Sep 4, 2018 at 9:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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