You are here

function file_entity_tokens in File Entity (fieldable files) 7

Same name and namespace in other branches
  1. 8.2 file_entity.tokens.inc \file_entity_tokens()
  2. 7.3 file_entity.tokens.inc \file_entity_tokens()
  3. 7.2 file_entity.tokens.inc \file_entity_tokens()

Implements hook_tokens().

File

./file_entity.tokens.inc, line 48
Token integration for the file_entity module.

Code

function file_entity_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $replacements = 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']);

  // File tokens.
  if ($type == 'file' && !empty($data['file'])) {
    $file = $data['file'];
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'type':
          if ($file_type = file_info_file_types($file->type)) {
            $replacements[$original] = $sanitize ? check_plain($file_type['label']) : $file_type['label'];
          }
          break;
      }
    }

    // Chained token relationships.
    if (($file_type_tokens = token_find_with_prefix($tokens, 'type')) && ($file_type = file_info_file_types($file->type))) {
      $file_type['type'] = $file->type;
      $replacements += token_generate('file-type', $file_type_tokens, array(
        'file_type' => $file_type,
      ), $options);
    }
  }

  // File type tokens.
  if ($type == 'file-type' && !empty($data['file_type'])) {
    $file_type = $data['file_type'];
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'name':
          $replacements[$original] = $sanitize ? check_plain($file_type['label']) : $file_type['label'];
          break;
        case 'machine-name':

          // This is a machine name so does not ever need to be sanitized.
          $replacements[$original] = $file_type['type'];
          break;
        case 'count':
          $query = db_select('file_managed');
          $query
            ->condition('type', $file_type['type']);
          $query
            ->addTag('file_type_file_count');
          $count = $query
            ->countQuery()
            ->execute()
            ->fetchField();
          $replacements[$original] = (int) $count;
          break;
        case 'edit-url':
          $replacements[$original] = url('admin/config/media/file-types/manage/' . $file_type['type'] . '/fields', $url_options);
          break;
      }
    }
  }
  return $replacements;
}