You are here

function filefield_token_values in FileField 6.3

Same name and namespace in other branches
  1. 5.2 filefield.module \filefield_token_values()
  2. 6.2 filefield.module \filefield_token_values()

Implementation of hook_token_values().

Provide the token values for a given file item.

File

./filefield.token.inc, line 36
Token hook implementations. Included if token.module is installed.

Code

function filefield_token_values($type, $object = NULL) {
  $tokens = array();
  if ($type == 'field' && isset($object[0]['fid'])) {
    $item = $object[0];
    $tokens['filefield-fid'] = $item['fid'];
    $tokens['filefield-description'] = isset($item['data']['description']) ? check_plain($item['data']['description']) : '';
    $tokens['filefield-filename'] = check_plain($item['filename']);
    $tokens['filefield-filepath'] = check_plain($item['filepath']);
    $tokens['filefield-fileurl'] = file_create_url($item['filepath']);
    $tokens['filefield-filemime'] = $item['filemime'];
    $tokens['filefield-filesize'] = $item['filesize'];
    $tokens['filefield-filesize_formatted'] = format_size($item['filesize']);
    $tokens['filefield-view'] = $item['view'];
    $double_extensions = array(
      'tar.gz',
      'tar.bz2',
    );
    foreach ($double_extensions as $ext) {
      $length = strlen($ext) + 1;
      $filename = drupal_strtolower($item['filename']);
      $pos = strrpos($filename, '.' . $ext);
      if ($pos !== FALSE && strlen($filename) - $pos == $length) {
        $tokens['filefield-onlyname'] = substr($item['filename'], 0, $pos);
        $tokens['filefield-extension'] = $ext;
        break;
      }
    }
    if (!isset($tokens['filefield-extension'])) {
      $info = pathinfo($item['filename']);
      $tokens['filefield-onlyname'] = basename($info['basename'], '.' . $info['extension']);
      $tokens['filefield-extension'] = $info['extension'];
    }
  }
  return $tokens;
}