1 entity.tokens.inc entity_tokens($type, $tokens, array $data = array(), array $options = array())

Implements hook_tokens().

File

core/modules/entity/entity.tokens.inc, line 48
Token callbacks for the entity module.

Code

function entity_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $sanitize = !empty($options['sanitize']);
  $replacements = array();
  if (!empty($data[$type]) && is_a($data[$type], 'Entity')) {
    $entity = $data[$type];

    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'url':
          if (token_get_module($type, 'url') == 'entity' && $uri = $entity->uri()) {
            $uri['options']['absolute'] = TRUE;
            $replacements[$original] = url($uri['path'], $uri['options']);
          }
          break;

        case 'original':
          if (!empty($entity->original)) {
            $label = $entity->original->label();
            $replacements[$original] = $sanitize ? check_plain($label) : $label;
          }
          break;
      }
    }

    // [entity:url:*] chained tokens.
    if (token_get_module($type, 'url') == 'entity' && $url_tokens = token_find_with_prefix($tokens, 'url')) {
      $replacements += token_generate('url', $url_tokens, $entity->uri(), $options);
    }

    // [entity:original:*] chained tokens.
    if (($original_tokens = token_find_with_prefix($tokens, 'original')) && !empty($entity->original)) {
      $replacements += token_generate($type, $original_tokens, array($type => $entity->original), $options);
    }
  }

  return $replacements;
}