Entity Token Support

Last updated on
14 October 2016

Drupal 7

Integration with the Token module is achieved by enabling "Entity Token", which is a sub-module that ships with the Entity module (in Drupal 7).
Relies on Token module.

Drupal 7 Entity Token Example

$type = 'node';
$eid = 1;
$inc = module_load_include('inc', 'entity_token', 'entity_token.tokens');
// print well formatted debugging info - disable if you do not have "Devel" module enabled
//dpm($inc,'result of module_load_include');
$tokens = entity_token_types();
echo "<b>Tokens Types: </b>" . implode(', ', $tokens) . "<hr>";
//dpm(arg()," entity_load_single($type, $eid)");
if ($type and $eid) {
  $e = entity_load_single($type, $eid);
  //dpm($e," entity_load_single($type, $eid)");
  $tokens = entity_token_types();
  //dpm($tokens,'tokens after entity load');

  $text = "Type: [$type:type] Name: [$type:title] User: [site:current-user:name]";
  $replace = token_replace($text, array($type => $e));
  echo $text . ' = ' . $replace;

}

Drupal 8

Drupal 8 Entity Token Example