5

I have a bunch of files that are displayed in a view; i'm using 'generic files' setting to display them; the problem is that some files have really ugly names like '1002-32-eda.pdf' i'd like to change the name to 'download' but keep the 'mime' detection (small icon on the left side; like pdf, word etc).

Any example on how to change this ?

5

I'm guessing that you are using a field to save the data about the files.

When the file is displayed in views, it's displayed through a formatter which acts like a theme function. The different ways an image can be shown, is in reality just different formatters that link to different theme functions (or the same, but with different arguments).

You have two options. * You can override the theme function, which would change how ALL files are shown, when displayed using 'generic files'. * Yo u can create your own formatter, and theme function which could be a copy of the one Generic files uses, but with a few alterations.

The hooks you need to use, depend on which version of Drupal you use, but you can search the modules and copy how it's done for Generic files.

For a long explaination you can check out this blog post I wrote a while back.

The short version is:

use Hook_field_formatter_info:

/**
 * Implementation of CCK's hook_field_formatter_info().
 */
function module_formatter_info() {
  return array(
    'default' => array(
      'label' => t('My generic files'),
      'field types' => array('filefield'),
      'multiple values' => CONTENT_HANDLE_CORE,
      'description' => t('My description.'),
    ),
  );
}

Next use hook_theme, creating a theme function with a hook like name:

function filefield_theme() {
  return array(
    'module_formatter_default' => array(
      'arguments' => array('element' => NULL),
    ),
  );
}

Lastly create the theme function

/**
 * Theme function for the 'default' filefield formatter.
 */
function theme_module_formatter_default($element) {
  $file = $element['#item'];
  $file['data']['description'] = t('Download');
  $field = content_fields($element['#field_name']);
  $output = theme('filefield_item', $file, $field);
  return $output;
}

The above code should work and do what you want, but haven't actually tested this.

  • Views 2 = Drupal 6 ;) added the correct tags – Alex Weber Apr 25 '11 at 10:04
  • @Alex Right, good catch. – googletorp Apr 25 '11 at 11:58
3

There is another option, if you want to do this for all files on the site: create an override for the function theme_file_link (found in the file file.module):

function THEMENAME_file_link($variables) {
  $file = $variables['file'];
  $icon_directory = $variables['icon_directory'];

  $url = file_create_url($file->uri);
  $icon = theme('file_icon', array('file' => $file, 'icon_directory' => $icon_directory));

  // Set options as per anchor format described at
  // http://microformats.org/wiki/file-format-examples
  $options = array(
    'attributes' => array(
      'type' => $file->filemime . '; length=' . $file->filesize,
    ),
  );

  // Use the description as the link text if available.
  if (empty($file->description)) {
    $link_text = $file->filename;
  }
  else {
    $link_text = $file->description;
    $options['attributes']['title'] = check_plain($file->filename);
  }

  // DO SOMETHING WITH THE LINK TEXT HERE, FOR EXAMPLE:
  // $link_text = urldecode($link_text);

  return '<span class="file">' . $icon . ' ' . l($link_text, $url, $options) . '</span>';
} 

The drawback is that all file links on the site will now look different, but in some cases that is precisely what you are after. Remember to flush all caches afterward, to make it all work :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.