Skip to main content
deleted 211 characters in body
Source Link

For Drupal 8, see How to use create custom tokens in sending mails in drupal 8 Programmatically, and the code it shows.For Drupal 8

// We need to include the needed class for tokens.

use Drupal\Core\Render\BubbleableMetadata;

/**
 * Implements hook_token_info().
 */
function modulename_token_info() {
  $info = array();
  // Add any new tokens.
  $info['tokens']['customtokentype']['customtoken'] = t('Telling drupal that you define custom token');
  // Return them.
  return $info;
}

/**
 * Implements hook_tokens().
 */
function modulename_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
  $replacements = array();
  $simple = $data["customanything"];
  if ($type == 'customtokentype') {
    foreach ($tokens as $name => $original) {
      // Find the desired token by name
      switch ($name) {
        case 'customtoken':
          $new = $simple;
          $replacements[$original] = $new;
          break;
      }
    }
  }   
  // Return the replacements.
  return $replacements;
}

Getting the value of tokens in your function requires code similar to the following one.

$token = \Drupal::token();
$message_html = "hello my custom token is replaced see it here [customtokentype:customtoken]";

// Token data.
$data = array('customanything' => $tosendtotokens);
$message_html = $token->replace($message_html, $data);

For Drupal 8, see How to use create custom tokens in sending mails in drupal 8 Programmatically, and the code it shows.

// We need to include the needed class for tokens.

use Drupal\Core\Render\BubbleableMetadata;

/**
 * Implements hook_token_info().
 */
function modulename_token_info() {
  $info = array();
  // Add any new tokens.
  $info['tokens']['customtokentype']['customtoken'] = t('Telling drupal that you define custom token');
  // Return them.
  return $info;
}

/**
 * Implements hook_tokens().
 */
function modulename_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
  $replacements = array();
  $simple = $data["customanything"];
  if ($type == 'customtokentype') {
    foreach ($tokens as $name => $original) {
      // Find the desired token by name
      switch ($name) {
        case 'customtoken':
          $new = $simple;
          $replacements[$original] = $new;
          break;
      }
    }
  }   
  // Return the replacements.
  return $replacements;
}

Getting the value of tokens in your function requires code similar to the following one.

$token = \Drupal::token();
$message_html = "hello my custom token is replaced see it here [customtokentype:customtoken]";

// Token data.
$data = array('customanything' => $tosendtotokens);
$message_html = $token->replace($message_html, $data);

For Drupal 8

// We need to include the needed class for tokens.

use Drupal\Core\Render\BubbleableMetadata;

/**
 * Implements hook_token_info().
 */
function modulename_token_info() {
  $info = array();
  // Add any new tokens.
  $info['tokens']['customtokentype']['customtoken'] = t('Telling drupal that you define custom token');
  // Return them.
  return $info;
}

/**
 * Implements hook_tokens().
 */
function modulename_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
  $replacements = array();
  $simple = $data["customanything"];
  if ($type == 'customtokentype') {
    foreach ($tokens as $name => $original) {
      // Find the desired token by name
      switch ($name) {
        case 'customtoken':
          $new = $simple;
          $replacements[$original] = $new;
          break;
      }
    }
  }   
  // Return the replacements.
  return $replacements;
}

Getting the value of tokens in your function requires code similar to the following one.

$token = \Drupal::token();
$message_html = "hello my custom token is replaced see it here [customtokentype:customtoken]";

// Token data.
$data = array('customanything' => $tosendtotokens);
$message_html = $token->replace($message_html, $data);
improved formatting; better included information in an existing sentence
Source Link
avpaderno
  • 97.9k
  • 15
  • 164
  • 284

Drupal 8 Example

For Drupal 8, see ForHow to use create custom tokens in sending mails in drupal 8 Programmatically

EVERYTHING CAN BE IN MODULENAME, and the code it shows.MODULE FILE

// We need to include the needed  class for tokens.

use Drupal\Core\Render\BubbleableMetadata;

 /**
  * Implements hook_token_info().
  */
function modulename_token_info() {
  $info = array();
  // Add any new tokens.
  $info['tokens']['customtokentype']['customtoken'] = t('Telling drupal that you define custom token');
  // Return them.
  return $info;
}

 /**
  * Implements hook_tokens().
  */
function modulename_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
   $replacements = array();
   $simple = $data["customanything"];
   if ($type == 'customtokentype') {
    foreach ($tokens as $name => $original) {
      // Find the desired token by name
      switch ($name) {
        case 'customtoken':
          $new = $simple;
          $replacements[$original] = $new;
          break;
      }
    }
   }   
  // Return the replacements.
  return $replacements;
}

CallingGetting the created customvalue of tokens in your functionsfunction requires code similar to the following one.

$token = \Drupal::token();
$message_html = "hello my custom token is replaced see it here [customtokentype:customtoken]";

// Token data.
$data = array('customanything' => $tosendtotokens);
$message_html = $token->replace($message_html, $data);

By: monkpal

Further If you want use token replaced custom email in drupal 8 please refer

Drupal 8 Example

For drupal 8

EVERYTHING CAN BE IN MODULENAME.MODULE FILE

// We need to include the needed  class for tokens

use Drupal\Core\Render\BubbleableMetadata;

 /**
  * Implements hook_token_info().
  */
function modulename_token_info() {
 $info = array();
 // Add any new tokens.
 $info['tokens']['customtokentype']['customtoken'] = t('Telling drupal that you define custom token');
// Return them.
 return $info;
}

 /**
  * Implements hook_tokens().
  */
function modulename_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
   $replacements = array();
   $simple = $data["customanything"];
   if ($type == 'customtokentype') {
    foreach ($tokens as $name => $original) {
  // Find the desired token by name
     switch ($name) {
      case 'customtoken':
      $new = $simple;
      $replacements[$original] = $new;
      break;
     }
    }
   }   
// Return the replacements.
 return $replacements;
}

Calling the created custom tokens in your functions

$token = \Drupal::token();
$message_html = "hello my custom token is replaced see it here [customtokentype:customtoken]";

// Token data.
$data = array('customanything' => $tosendtotokens);
$message_html = $token->replace($message_html, $data);

By: monkpal

Further If you want use token replaced custom email in drupal 8 please refer

For Drupal 8, see How to use create custom tokens in sending mails in drupal 8 Programmatically, and the code it shows.

// We need to include the needed class for tokens.

use Drupal\Core\Render\BubbleableMetadata;

/**
 * Implements hook_token_info().
 */
function modulename_token_info() {
  $info = array();
  // Add any new tokens.
  $info['tokens']['customtokentype']['customtoken'] = t('Telling drupal that you define custom token');
  // Return them.
  return $info;
}

/**
 * Implements hook_tokens().
 */
function modulename_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
  $replacements = array();
  $simple = $data["customanything"];
  if ($type == 'customtokentype') {
    foreach ($tokens as $name => $original) {
      // Find the desired token by name
      switch ($name) {
        case 'customtoken':
          $new = $simple;
          $replacements[$original] = $new;
          break;
      }
    }
  }   
  // Return the replacements.
  return $replacements;
}

Getting the value of tokens in your function requires code similar to the following one.

$token = \Drupal::token();
$message_html = "hello my custom token is replaced see it here [customtokentype:customtoken]";

// Token data.
$data = array('customanything' => $tosendtotokens);
$message_html = $token->replace($message_html, $data);
added 1447 characters in body
Source Link

Drupal 8 Example

For drupal 8

Creating custom tokens and sendingEVERYTHING CAN BE IN MODULENAME.MODULE FILE

// We need to include the needed  class for tokens

use Drupal\Core\Render\BubbleableMetadata;

 /**
  * Implements hook_token_info().
  */
function modulename_token_info() {
 $info = array();
 // Add any new tokens.
 $info['tokens']['customtokentype']['customtoken'] = t('Telling drupal that you define custom token');
// Return them.
 return $info;
}

 /**
  * Implements hook_tokens().
  */
function modulename_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
   $replacements = array();
   $simple = $data["customanything"];
   if ($type == 'customtokentype') {
    foreach ($tokens as $name => $original) {
  // Find the desired token by name
     switch ($name) {
      case 'customtoken':
      $new = $simple;
      $replacements[$original] = $new;
      break;
     }
    }
   }   
// Return the replacements.
 return $replacements;
}

Calling the created custom tokens in email body drupal 8 your functions

$token = \Drupal::token();
$message_html = "hello my custom token is replaced see it here [customtokentype:customtoken]";

// Token data.
$data = array('customanything' => $tosendtotokens);
$message_html = $token->replace($message_html, $data);

By: monkpal

click hereFurther If you want use token replaced custom email in drupal 8 please refer

Drupal 8 Example

For drupal 8

Creating custom tokens and sending the custom tokens in email body drupal 8 click here

Drupal 8 Example

For drupal 8

EVERYTHING CAN BE IN MODULENAME.MODULE FILE

// We need to include the needed  class for tokens

use Drupal\Core\Render\BubbleableMetadata;

 /**
  * Implements hook_token_info().
  */
function modulename_token_info() {
 $info = array();
 // Add any new tokens.
 $info['tokens']['customtokentype']['customtoken'] = t('Telling drupal that you define custom token');
// Return them.
 return $info;
}

 /**
  * Implements hook_tokens().
  */
function modulename_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
   $replacements = array();
   $simple = $data["customanything"];
   if ($type == 'customtokentype') {
    foreach ($tokens as $name => $original) {
  // Find the desired token by name
     switch ($name) {
      case 'customtoken':
      $new = $simple;
      $replacements[$original] = $new;
      break;
     }
    }
   }   
// Return the replacements.
 return $replacements;
}

Calling the created custom tokens in your functions

$token = \Drupal::token();
$message_html = "hello my custom token is replaced see it here [customtokentype:customtoken]";

// Token data.
$data = array('customanything' => $tosendtotokens);
$message_html = $token->replace($message_html, $data);

By: monkpal

Further If you want use token replaced custom email in drupal 8 please refer

Source Link
Loading