Drupal Create Custom Tokens And Use It In Editor

Tokens provides an input filter that replaces tokens with content. All tokens are binded to its function that returns content as a value for that token. Token can contain parameters that will be passed to its function.

To create custom token, three token api functions are used:

/**
 * Implements hook_token_info().
 */
function MYMODULE_token_info() {
  // Define a new token type.
  $types['custom'] = array(
    'name' => t("Custom Tokens"),
    'description' => t("Tokens that are custom."),
  );


  // Define any new tokens.
  $tokens['fname'] = array(
    'name' => t("First name"),
    'description' => t("First name token"),
  );


  $tokens['lname'] = array(
    'name' => t("Last name"),
    'description' => t("Last name token"),
  );


  return array(
    'types' => $types,
    'tokens' => array(
      'tn' => $tokens,
    ),
  );
}

We are creating custom token type here. Get a list of the existing tokens and types by calling token_get_info() to add token to existing types.

/**
 * Implements hook_tokens(). This hook will operate the token and replace it with it's value.
 */
function MYMODULE_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $replacements = array();


  if ($type == 'custom') {
    // Loop through each of the available tokens.
    foreach ($tokens as $name => $original) {
      // Find our custom tokens by name.
      switch ($name) {
        case 'fname':
          // Work out the value of our token.
          $value = 'Deepali';
          // Give our token it's value!
          $replacements[$original] = $value;
          break
        case 'lname':
          // Work out the value of our token.
          $value = 'Agarwal';
          // Give our token it's value!
          $replacements[$original] = $value;
          break;
      }
    }
  }
  return $replacements;
}

Custom created token will now be available in the list of tokens. You can use Token Insert module to insert tokens into a textarea (plain text and wysiwyg textareas).

ARGUMENTS

You can pass additional contextual data to token_replace() function which is then passed to implementations of hook_tokens(). You can also use below code to pass the values of token dynamically.

/**
 * Implements hook_tokens(). This hook will operate the token and replace it with it's value.
 */
function MYMODULE_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $replacements = array();


  if ($type == 'custom') {
    // Loop through each of the available tokens.
    foreach ($tokens as $name => $original) {
      // Replace token with desired values
      if (array_key_exists($name, $data)) {
        $replacements[$original] = $data[$name];  
      }
    }
  }
  return $replacements;
}


/**
 * Here we will use the token_replace() function to get the actual content after replacement.
 */
function MYMODULE_MYFUNCTION(&$variables) {
  $data = array();
  $data['fname'] = 'Deepali';
  $data['lname'] = 'Agarwal';
  $temp = "Full name : [custom:fname] [custom:lname]";
  $temp = token_replace($temp, $data);
  echo $temp;
  // After token replacement $temp variable will contain "Full name : Deepali Agarwal"
}

The $data parameter can contain either the $user object or the $node object required to calculate the appropriate values.

Feel free to share your valuable inputs/feedbacks. #letstalksolution and need more assistance regarding Drupal Web Development contact us now.

Floating Icon 1Floating Icon 2