You are here

function relation_tokens in Relation 7

Same name and namespace in other branches
  1. 8.2 relation.tokens.inc \relation_tokens()
  2. 8 relation.tokens.inc \relation_tokens()

Implements hook_tokens().

File

./relation.tokens.inc, line 74
Builds placeholder replacement tokens for relation data.

Code

function relation_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $url_options = array(
    'absolute' => TRUE,
  );
  if (isset($options['language'])) {
    $url_options['language'] = $options['language'];
    $language_code = $options['language']->language;
  }
  else {
    $language_code = NULL;
  }
  $sanitize = !empty($options['sanitize']);
  $replacements = array();
  if ($type == 'relation' && !empty($data['relation'])) {
    $relation = $data['relation'];
    $relation_uri = entity_uri('relation', $relation);
    foreach ($tokens as $name => $original) {
      switch ($name) {

        // Simple key values on the relation.
        case 'rid':
          $replacements[$original] = $relation->rid;
          break;
        case 'vid':
          $replacements[$original] = $relation->vid;
          break;
        case 'relation_type':
          $replacements[$original] = $sanitize ? check_plain($relation->relation_type) : $relation->relation_type;
          break;
        case 'type-name':
          $type_label = relation_get_type_label($relation);
          $replacements[$original] = $sanitize ? check_plain($type_label) : $type_label;
          break;
        case 'url':
          $replacements[$original] = url($relation_uri['path'], $url_options + $relation_uri['options']);
          break;
        case 'edit-url':
          $replacements[$original] = url($relation_uri['path'] . '/edit', $url_options + $relation_uri['options']);
          break;

        // Default values for the chained tokens handled below.
        case 'author':
          if ($relation->uid == 0) {
            $name = variable_get('anonymous', t('Anonymous'));
          }
          else {
            $user = user_load($relation->uid);
            $name = $user->name;
          }
          $replacements[$original] = $sanitize ? filter_xss($name) : $name;
          break;
        case 'created':
          $replacements[$original] = format_date($relation->created, 'medium', '', NULL, $language_code);
          break;
        case 'changed':
          $replacements[$original] = format_date($relation->changed, 'medium', '', NULL, $language_code);
          break;
        default:
          if (substr($name, 0, 10) == 'endpoints-') {

            // These tokens are created by entity module from the entity properties.
            // See relation_entity_property_info_alter().
            list(, $endpoint_type, $entity_type) = explode('-', $name);
            $endpoints = relation_get_endpoints_by_endpoint_type($relation, $endpoint_type);
            $endpoint = array_shift($endpoints);
            $replacements[$original] = isset($endpoint['entity_id']) ? $endpoint['entity_id'] : 0;
          }
      }
    }
    if ($author_tokens = token_find_with_prefix($tokens, 'author')) {
      $author = user_load($relation->uid);
      $replacements += token_generate('user', $author_tokens, array(
        'user' => $author,
      ), $options);
    }
    if ($created_tokens = token_find_with_prefix($tokens, 'created')) {
      $replacements += token_generate('date', $created_tokens, array(
        'date' => $relation->created,
      ), $options);
    }
    if ($changed_tokens = token_find_with_prefix($tokens, 'changed')) {
      $replacements += token_generate('date', $changed_tokens, array(
        'date' => $relation->changed,
      ), $options);
    }
  }
  return $replacements;
}