When setting the filename token, I see a [file:description] token, but it does not work when the filename is updated. Looking into this a little more closely, it appears that the file description is stored in the field_data_ tables, and is not loaded with file_load().

The attached patch adds the description into the token data, so that it can be used. I did this by adding an "additional" array to the file data, which other modules can use to merge file token data.

CommentFileSizeAuthor
add-file-descriptions.patch2.21 KBjoelstein

Comments

joelstein’s picture

Oh, I also switched to using field_info_fields(), to resolve the outstanding todo. Now, file_filefield_paths_get_fields looks a lot more like image_filefield_paths_get_fields.

dave reid’s picture

Status: Needs review » Needs work

We officially removed this token from core as it doesn't actually work, so you shouldn't work to support it. The proper way to support the description data is with Field token support - not supporting it for the file token type.

h0tw1r3’s picture

Status: Needs work » Postponed

Thanks for commenting on this Dave. Putting on hold until #691078: Field tokens is worked out.

joelstein’s picture

As of Drupal 7.6, [file:description] has been removed from core. See #978028: File token fixes: [file:description] does not exist and [file:owner] not using format_username.

In case anyone needs a temporary solution (like me), you can add this token in a custom module.

/**
 * Implements hook_token_info().
 * 
 * Exposes the [file:description] token, which is no longer part of core, and
 * which cannot be implemented in Token until the following issues are
 * resolved, and which point this and custom_tokens should both be removed.
 * 
 * @see #1225762
 * @see #691078
 */
function custom_token_info() {
  $info['tokens']['file']['description'] = array(
    'name' => t('File description'),
    'description' => t('An optional human-readable description of the file.'),
  );
  return $info;
}

/**
 * Implements hook_tokens().
 * 
 * @see custom_token_info()
 */
function custom_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $replacements = array();
  $sanitize = !empty($options['sanitize']);
  if ($type == 'file' && !empty($data['file']) && !empty($data['node'])) {
    $fields = field_info_fields();
    foreach ($fields as $name => $field) {
      if ($field['type'] == 'file' && isset($data['node']->{$field['field_name']}) && is_array($data['node']->{$field['field_name']})) {
        foreach ($data['node']->{$field['field_name']} as $language) {
          foreach ($language as $file) {
            if ($file['fid'] == $data['file']->fid) {
              $replacements[$tokens['description']] = $sanitize ? check_plain($file['description']) : $file['description'];
            }
          }
        }
      }
    }
  }
  return $replacements;
}
deciphered’s picture

Status: Postponed » Closed (won't fix)

Closing this due to the fact that the token in question no longer exists.

If the custom implementation of the token doesn't work, then happy enough to re-open.

dead_arm’s picture

Issue summary: View changes

I came across this issue while trying to render the description field of a file with a token, and was disappointed to find that it wasn't possible.

We found a solution though. I should mention we are using Display Suite, and by enabling the description field on the file field, and then using the "Generic file" format for the field display, we achieved what we wanted, which was to have the text that is input into the description field display as the title of the uploaded file, without the filename extension.

steveoriol’s picture

I like the temporary solution on #4, thank you joelstein

fallenturtle’s picture

What version of token_custom do I need to make the code in #4 work? I've tried both 7.x-1.1 and 7.x-2.0-beta3 with no success.

Nevermind.