Use profile field as token
2011 10 Sep

Use profile fields as tokens in user emails in Drupal

In Drupal, many a time I have found the need of using user profile values in my email configurations at "User Settings" and let the client use that too. The need for accessing Drupal user fields as tokens in user emails can be addressed with a very small piece of code. Let's say we need the tokens of profile first name, last name, full name and profile title like 'Mr.', 'Mrs.', etc, here is what we can do ...In Drupal, many a time I have found the need of using user profile values in my email configurations at "User Settings" and let the client use that too. The need to include custom tokens in the user registration emails can be addressed with a very small piece of code. Let's say we need the tokens of profile first name, last name, full name and profile title like 'Mr.', 'Mrs.', etc, here is what we can do:

  1. Alter the user settings form using hook_form_alter to mention the tokens that you want to use
  2. Now, we need to implement the hook mail alter to replace the tokens in the message and the subject of configured email

In Drupal, many a time I have found the need of using user profile values in my email configurations at "User Settings" and let the client use that too. The need for tokens in user emails can be addressed with a very small piece of code. Let's say we need the tokens of profile first name, last name, full name and profile title like 'Mr.', 'Mrs.', etc, here is what we can do:


1.Alter the user settings form using hook_form_alter to mention the tokens that you want to use

<?php /** * Implementation of hook_form_alter(). */ 
function example_form_alter(&amp;$form, &amp;$form_state, $form_id) {
  switch($form_id) {
    case 'user_admin_settings':
      if (is_array($form['email']) &amp;&amp; $form['email']['#type'] == 'fieldset') {
        foreach ($form['email'] AS $key =&gt; $value) {
          if (is_array($value) &amp;&amp; $value['#type'] == 'fieldset') {
            $form['email'][$key]['#description'] .= '<br />' . t('Also available variables are: ') . '!profile_first_name, !profile_last_name, !profile_full_name, !profile_title';
          }
        }
      }
      break;
  }
} 
?> 

2. Now, we need to implement the hook mail alter to replace the tokens in the message and the subject of configured email

&lt;?php
/**
 * Implementation of hook_mail_alter().
 */
function example_mail_alter(&amp;$message) {
  switch ($message['id']) {
    case "user_register_admin_created":
    case "user_register_no_approval_required": 
    case "user_register_pending_approval":
    case "user_password_reset": 
    case "user_status_blocked":
    case "user_status_deleted":
    case "logintoboggan_register_no_approval_required":
      $token_replacements = array(
        '!profile_first_name' =&gt; $message['params']['account']-&gt;profile_first_name,
        '!profile_last_name' =&gt; $message['params']['account']-&gt;profile_last_name,
        '!profile_full_name' =&gt; $message['params']['account']-&gt;profile_first_name . ' ' . $message['params']['account']-&gt;profile_last_name,
        '!profile_title' =&gt; $message['params']['account']-&gt;profile_title,
      );
      // You can also use $message['params']['account']-&gt;uid to get more values related to the user like number of new messages (privatemsg) he/she got on the site
      $message['subject'] = strtr($message['subject'], $token_replacements);
      $message['body'][0] = strtr($message['body'][0], $token_replacements);
      break;
  }
}
?&gt;

There are more ways than one to solve the same problem, and the same holds true for Drupal. Because in this case, the code gives me exactly what I need, no less no more, and is a short and easily comprehensive piece of code, I prefer it over any contributed module which offers me too much for a small piece of requirement.

Latest Blogs

Best Practices for Software Testing

Power of Software Testing: Why Software Teams Use It Effectively

In the modern digital era, where software is used in nearly every aspect of everyday life, the importance of software testing cannot be emphasized.

Read More

Benefits of Programmatic SEO Implementation

What It Is and How to Implement It How SEOs Make the Web Better

In the rapidly evolving field of SEO, staying ahead of the curve necessitates adopting new strategies and utilizing technology.

Read More

Unlocking the potential of SEO for boosting business

Branding With SEO: Boost brand Visibility, credibility, and Engagement With An SEO Strategy

Many people think that the primary function of search engine optimization is to garner organic traffic on a website; however, it is beyond that.

Read More

Future-proofing your Mobile UI

Future-proofing your Mobile UI: Design Trends and Insights For Lasting Success

2023 was a revolutionizing year in the user interface designing field. Some events also showed substantial changes in how technology is used.

Read More