Access entity field with custom token

Access entity field with custom token

Once working on the Drupal project, I faced a situation when I had to access the current loaded node fields in Rules. To be more specific, the content type had an email field and attached entity form. The task was to send the email using Rules to the email address which was set in a node field after submission the attached entity form.

The problem is that the only available fields I had were entity form fields and no fields from the node. So I came up to some custom and quick solution of creating custom Token which would contain the data from the node field.

I'm sure maybe there is a solution without custom coding, but at that point it had to be done quickly and I had not much time for researching it.

The first thing I checked the content type structure to make sure that the Email field is hidden.

node structure
I have a hidden Email field and Entity form attached in my content type.

The second, I inspected the node object using Devel module to see the structure of my email field there. It can be found by its machine name field_email.

Node object structure
Node object structure.

The next thing I did was creating field_helper folder and placing it into sites/all/modules/custom. Actually field_helper is the name of the module. Then I created module files: and field_helper.module and put them into my created folder.

Here is the source code of these files:

name = Field Token Helper
description = "Creates custom token from field's value"
package = Custom
core = 7.x
dependencies[] = token


 * Implements hook_token_info().
function field_helper_token_info() {

// Define email_field token, it will be available as [site:email_field]
  $field['email_field'] =  array(
    'name' => t("Email field token"),
    'description' => t("Email field value from the current node"),

// Return associative array of tokens & token types
  return array(
    'tokens' => array(
      'site' => $field,

 * Implements hook_tokens().
function field_helper_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $replacements = array();
// Check token type
  if($type == 'site'){
    foreach ($tokens as $name => $original) {
      switch ($name) {
// Check token name
        case 'email_field':
          $node = menu_get_object();
          $email_field = "";
          if (isset($node->field_email[LANGUAGE_NONE][0]['email'])) {
            $email_field = $node->field_email[LANGUAGE_NONE][0]['email'];
          $replacements[$original] = $email_field;
  return $replacements;

This is pretty simple as you can see, I created custom token email_field which would be available under site type and then I loaded current node with menu_get_object() function and saved the data from the node email field (field_email machine name) into my custom token.

Custom token code
Getting the proper field data from the node object.

After enabling newly created module I can see that my custom token is available under Rules interface.

Custom token is available
Custom token is available in the list and can be used for email TO value.

I also checked my token value with adding additional Show a message on the site action which displayed it via system message after submitting entity form in Rules.

Checking the token value
Checking the token value.

Make sure that you deleted this action after testing your token). 
That's all, this is how you can programmatically create a custom tokens with the data and logic you need and then use it in your Drupal projects.

Add new comment

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.