0

I have a File field in drupal 7 (with file module) which I forced with views to display as an icon and name of the file which uploaded. The Name displays as a link (to download file). Now I want to link the icon to the file too. So when users click on the icon they would be available to download file (and I prefer to exclude the name from display).
What should I do now to make the icon as a download link?
Thanks.

2
  • 1
    how did you "force" views to display the icon? – Jimajamma Jan 29 '12 at 19:28
  • actually I didn't force views, sorry. I set Display Field Format to: File Force: Generic file, and it prints an icon with a link text! – Sheshkovsky Jan 29 '12 at 19:48
2

I guess the problem is that 'html' in $options is set to FALSE by default, thus stripping the icon html code generated by theme().

I tried modifying a bit the code above and it worked perfectly:

/**
 * Implements theme_file_link().
 */
function yourtheme_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,
    ),
  );

  // Updating 'html' to TRUE so that the html code to display the icon is     printed
  $options += array(
    'html' => TRUE,
  );


  // 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);
  }

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

Note that I added 3 lines (plus a commented line, 4) to update the value of HTML.

1
/**
 * Implements theme_file_link().
 */
function yourtheme_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);
  }

  return '<span class="file">' . l($icon, $url, $options) . ' ' . l($link_text, $url, $options) . '</span>';
}
3
  • place it in your active theme's template.php then clear the cache, refresh. – gilzero Jan 30 '12 at 2:42
  • it doesn't work for me:( I've found similar code in file.module file (of course instead of two lines,last one and 8th). shouldn't I change that file too? And I have change yourtheme to bartik which is my default template. Your code seems to be worked, but there should be a problem, can you define it? – Sheshkovsky Jan 31 '12 at 2:10
  • No. You should never mod any core file including the file.module. What I post is implementation of theme_file_link, which overrides the default theme_file_link defined in file.module. – gilzero Jan 31 '12 at 5:19

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.