Hi all,
I'm trying to obtain a simple result on a view related to fielfields, but I can't find a simple solution.
I want to show a view table with the list of files attached to a node (attached using filefield). I can insert in this table every information related to the file (name, size, mimetype, data uploas, description, ...) but I can't show the icon for this file (and I think this is a quite natural thing).
I have searched for an answer on issues, but I have found nothing related to a view.
Someone can explain if this thing is possible, and if the answer is positively, how?
Thanks a lot,
Saxx

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

quicksketch’s picture

Sounds like you've created a relationship for "File: Name of your field" to the files table, which is getting you all the meta information (name, size, mimetype, etc.) which is great. If you want to output the name of the file with the nice little icon that comes with FileField, then don't use the "File: Filename" field, instead use "Content: Name of your field (field_name)", so that CCK outputs the filename after running it through the formatter.

SaxxIng’s picture

Yes, it's right!
But I need to represent only the icon on my table (on a separate column respect to file_name). For example (headers of hypothetically table):

icon | mime_type | file_name | file_description | file_size | node_links

Generally this is a quite usual thing on many sites.
Do you think this is possible with the actually file_field fields (and integration with views)? If no, do you think this can be a reasonable feature request?
Thanks a lot for the rapid answer and for the great module.
Regards,
Saxx

quicksketch’s picture

Category: support » feature

This looks like something that could be possible, since the theme_filefield_icon() is solely responsible for outputting a file icon. However the FileField Views implementation does not add this particular support, so we should move this to a feature request. Note I don't have any timeline or plans to implement this myself, but I could see it as a valuable feature which I'd be happy to include if developed.

Golem07’s picture

Very good feature request.

xjm’s picture

Tracking.

mattiasj’s picture

subscribing

czeky’s picture

Yeah, to display just an clickable icon would be great, I'm need this as well

xjm’s picture

Alright, I spent four hours trying to add a field handler to filefield that would do this, but it just wasn't working. My views-fu is not good enough, it appears. However, I think I'm onto how to do this in the theming layer using filefield's theming includes:

  1. Add a Content: Fieldname (field_fieldname) - fid relationship to the view.
  2. Add a File: Mime Type field to your view using this relationship. Be sure to blank out the label when configuring this field and use only the default rendering options.
  3. Create a views field template for the field and stick it in your theme directory. (To theme all File: Mime Type fields everywhere, you'd name it views-view-field--filemime.tpl.php. See http://www.group42.ca/theming_views_2_the_basics for help deciding what to name the file.)
  4. Put the following code in that template file:
    $theme_file = drupal_get_path('module', 'filefield') ."/filefield.theme.inc";
    $mime = $row->{$field->field_alias};
    if (is_file($theme_file) && $mime) {
      require_once $theme_file;
    
      $file = array();
      $file['filemime'] = $mime;
      $icon = theme_filefield_icon($file);
      print $icon;
    }
    else {
      print $output;
    }
    
  5. In your view, click on "Theme information" on the left and then click the "Rescan template files" button.

Ta-da! You might want to do more with the markup or CSS (e.g. add a link around $icon to make it clickable), but this should get you started.

boabjohn’s picture

@xjm : mate you totally rock! This little fix works a treat and addresses a need I've spent days trying to figure out with no tech skills at all :-{)

BUT: My view comes up WITH nice icons, but NOT LINKED to the file download.

As I see it, this should be covered by the Views option to "Link this field to download the file", which I have ticked.

I note your code above just says to print $icon, and assume Views should be doing the rest... but for me, this doesn't happen.

The

is rendered like this:
 <td class="views-field views-field-filemime">
            <div class="filefield-icon field-icon-application-pdf"><img class="field-icon-application-pdf" alt="application/pdf icon" src="http://sample.site/sites/all/modules/filefield/icons/protocons/16x16/mimetypes/application-pdf.png"></div>          </td>

Any clue what I could be missing here? What else can I provide to help troubleshoot?

xjm’s picture

Since we're overriding the output of the field, we need to add the link back to the overridden output, too. If you check Link this field to download the file on the mimetype field, then it looks like the path to the file will be in the variable $row->{$field->aliases['filepath']} in the template file. So, instead of print $icon in the template above, you'd need to do something like:

print l($icon, $row->{$field->aliases['filepath']}, array('html' => TRUE));

This should render an HTML link to the file around the image.

boabjohn’s picture

Status: Active » Reviewed & tested by the community

Thanks heaps. Marking this reviewed.

quicksketch’s picture

Status: Reviewed & tested by the community » Active

As a "feature request" this isn't suitable to be included in FileField. While it's nice to have as documentation, the intention was to make accessing the icon alone possible directly through Views, without any theming. So I'm setting this back to active because there isn't any code that can be applied to the FileField project yet.

boabjohn’s picture

Sorry quicksketch, still new to issue protocols.

@xjm: the template actually isn't working for me. My view prints out the text: "application/pdf" and the text is not linked.

The template file is:

<?php
$theme_file = drupal_get_path('module', 'filefield') ."/filefield.theme.inc";
$mime = $row->{$field->field_alias};
if (is_file($theme_file) && $mime) {
  require_once $theme_file;

  $file = array();
  $file['filemime'] = $mime;
  $icon = theme_filefield_icon($file);
  print l($icon, $row->{$field->aliases['filepath']}, array('html' => TRUE));
}
else {
  print $output;
}
?>

The view appears to know about the template (ie, it appears in the Theme Information listing)

All caches and browsers flushed

There are no strange entries in Watchdog

The icons actually exist at: /sites/all/modules/filefield/icons/protocons/16x16/mimetypes/application-pdf.png

Is there some diagnostic I can use to trace the problem?

xjm’s picture

The template is coded to print the icon only if it can locate the theme file and the mimetype is set (if statement), and to use the view's default output otherwise (else statement). So, you could do some basic PHP troubleshooting to see what's happening and what the variables contain. You can put
print $variable;
at any point in the code to see what the value of the variable $variable is, or even something like
print "Got to this step.";
to see what branch of the code you're in. (It will be printed in the field in your view during your troubleshooting.)

Edit: The article I linked above (http://www.group42.ca/theming_views_2_the_basics) has some more generalized help for developing theming templates.

Regarding #12, for anyone who's interested, the logic could just as easily be used in a field handler (switch "print" for "return," etc.); I just couldn't figure out how to cleanly add a second handler for the file mimetype that depended on the filefield relationship. So, if someone figures out how to do that, I could roll a patch.

czeky’s picture

Hi, for me it seems to be working! except the link contain translation code in the link

http://www.xy.com/en/sites/default/files/poster.pdf

and should be... (without language code /en)

http://www.xy.com/sites/default/files/poster.pdf

any ideas?

also, when there are more files in the list, the are not displayed

but anyway good way

xjm’s picture

Aye, I assumed there was only one file; the code would need to be modified to support multiple values on the filefield. Not sure exactly how without looking into it more.

Regarding the lanugage prefix being added... that seems to be added by the l() or url() API functions. Both of these call language_url_rewrite() which does some funny business with the path based on language configuration.

Reflecting, I think maybe it should use file_create_url() instead, replacing:

print l($icon, $row->{$field->aliases['filepath']}, array('html' => TRUE));

with something like:

$file_url = file_create_url($row->{$field->aliases['filepath']});
print "<a href=\"$file_url\">$icon</a>";

If that doesn't work, as a (hackish) workaround, you could try manually coding your URL:

print "<a href=\"http://mysite.com/" . $row->{$field->aliases['filepath']} . "\">$icon</a>";
czeky’s picture

first code gives views AJAX error "An error occurred at /cs/admin/build/views/ajax/preview/media." The actual page is blank, second code works

xjm’s picture

Sorry, I made a typo. It should have read:
$file_url = file_create_url($row->{$field->aliases['filepath']});

I'll fix it in the post above for anyone else who wishes to test it.

jmuessig’s picture

subscribe

Vasu’s picture

subscribing

Cyberwolf’s picture

Subscribing.

Cyberwolf’s picture

I believe the right way to solve this is simply adding a new filefield field formatter that only displays the icon.

xjm’s picture

There is also a patch that allows another workaround:
#718832: Add file extension as a field available within the File group

You'd "rewrite output of this field" with the file extension to render the icon with markup or CSS as appropriate.

quicksketch’s picture

Version: 6.x-3.1 » 6.x-3.7

I believe the right way to solve this is simply adding a new filefield field formatter that only displays the icon.

I think a better option would be to create a Views handler. Not many people want to show just the icon without a label of any sort anywhere else. I imagine this scenario really only comes up when people want to put the icon in its own table cell or something.

grasmash’s picture

I just use Views PHP Customfield to grab file information:


//print var_export($data, TRUE);  //prints out all available fields in view

$value = $data->node_data_field_document_field_document_fid; //relevant fid

if ($value != NULL){
      
      $file = field_file_load($value);
      //print_r($file); //prints out various rows for file array
      $filepath = base_path().$file['filepath'];
      $filename = $file['filename'];
      $icon = theme('filefield_icon', $file);
      $filesize = format_size($file['filesize']);    
      $link = "<a href='$filepath'>$filename</a>";
      
      //print "<div class='file-attachment'>".$icon." ".$link." ".$filesize."</div>";
      print $icon;
}

php5engineer’s picture

FileSize
33.54 KB

In most basic case where are no need for any additional module or php code. See a configuration of a views field on the attached picture

xjm’s picture

#26: Your configuration does not provide the filefield icons appropriate to the file types.

quicksketch’s picture

Version: 6.x-3.7 » 6.x-3.9
Status: Active » Fixed
FileSize
1.84 KB

I've added this new Views handler for pulling in a file icon based on its MIME type. Note that this is only available if you've added a relationship over to the files table (usually by adding a "Content: Name of filefield" relationship to your view). Then a new field called "File: Icon" will be available. Since file icons don't require any actual FileField information, this handler will work on all files, including ones not uploaded by FileField. FileField just so happens to have a nice collection of icons that would be suitable for providing this option.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

AdrianB’s picture

If anyone using Drupal 7 finds quicksketch's comment #28 above and wonder how to do this in Drupal 7: There is no "File: Icon" field, instead, add a "File: Mime type" field and then check the checkbox Display an icon representing the file type, instead of the MIME text (such as "image/jpeg").

cduwe’s picture

What module provides the "File: Mime type" field to views? I don't seem to have that option.

szt’s picture

@cduwe: you must start the view-creation process with "Files" (not with "Content") at admin/structure/views/add.

ice5nake’s picture

I kind of wish that an empty file relationship would not display an icon. I am mixing node types in a View and I want nodes with a FileField to display the icon but I don't want to display the icon if the node doesn't have a FileField.

I imagine I can do this with a field theme template, but at first I kind of expected "Hide if empty" to exclude the icon if there was no file relationship.

dwightaspinwall’s picture

@cduwe and @szt - not necessary to re-roll the view starting as "Files" type. You can add a "File: Usage" relationship on the file field in question, then the File: MIME Type will be available to display as a field. Do that, and check "Display and icon..." in the field config.