You are here

function _entity_token_get_token in Entity API 7

Gets the token replacement by correctly obeying the options.

1 call to _entity_token_get_token()
entity_token_tokens in ./entity_token.tokens.inc
Implements hook_tokens().

File

./entity_token.tokens.inc, line 297
Provides tokens for entity properties which have no token yet.

Code

function _entity_token_get_token($wrapper, $options) {
  if (!$wrapper || $wrapper
    ->value() === NULL) {

    // Do not provide a replacement if there is no value.
    return NULL;
  }
  if (empty($options['sanitize'])) {

    // When we don't need sanitized tokens decode already sanitizied texts.
    $options['decode'] = TRUE;
  }
  $langcode = isset($options['language']) ? $options['language']->language : NULL;

  // If there is a label for a property, e.g. defined by an options list or an
  // entity label, make use of it.
  if ($label = $wrapper
    ->label()) {
    return empty($options['sanitize']) ? $label : check_plain($label);
  }
  switch ($wrapper
    ->type()) {
    case 'integer':
      return $wrapper
        ->value();
    case 'decimal':
      return number_format($wrapper
        ->value(), 2);
    case 'date':
      return format_date($wrapper
        ->value(), 'medium', '', NULL, $langcode);
    case 'duration':
      return format_interval($wrapper
        ->value(), 2, $langcode);
    case 'boolean':
      return $wrapper
        ->value() ? t('true') : t('false');
    case 'uri':
    case 'text':
      return $wrapper
        ->value($options);
  }

  // Care for outputting list values.
  if ($wrapper instanceof EntityListWrapper) {
    $output = array();
    foreach ($wrapper as $item) {
      $output[] = _entity_token_get_token($item, $options);
    }
    return implode(', ', $output);
  }

  // Else we do not have a good string to output, e.g. for struct values. Just
  // output the string representation of the wrapper.
  return (string) $wrapper;
}