Hi there,

I have created a content type for documents uploaded on to my page. The content type is then displayed in a tabled view the includes columns such as rating, upload date, title etc... I would like to add a field that displays the file extension of the file that was uploaded which would allow the user to sort by file extension type.

Is there an easy way to do this in views that doesn't require php coding?

I apologize if this is something that has been covered extensively somewhere else, but I can't seem to find a solution anywhere.

Thanks in advance!

Comments

On the right under "Advanced"

On the right under "Advanced" click on "Add" next to "Relationships".

Add a relationship for File Usage:File.

Once you've done that, you can add the field called File:Extension.

Drop Down Filter

Thanks bander2! That was very helpful!

Is there an easy way to create a radio button filter for that field? I can't seem to find File:Extension under the list in "Add filter criteria"

Thanks again!

Huh...

That is weird. You can add it as a field, but not a filter.

I am not sure where to send you on figuring that out. You might have to create a custom filter.

Sorry, I figured once you had the field displayed, you'd be home free.

One last question

No worries, I really appreciate the help.

I just have one last question, I can't seem to make the field sortable in a table view. Do you know of an easy way to fix that?

Thanks again!

Nope

I believe for the same reason that you can't filter on it. Upon further investigation it seems that Views cannot filter or sort on data that is not in the database. The file extension seems to be an attribute of the file entity that is not stored in the database, but is instead is calculated on the fly (I guess - I couldn't find the documentation)

So you have to either figure out a way to get the file extension into the database or override a bunch of views stuff. I got it to work by creating a field on my node called "Extension" and hiding it on the node form with hook_form_alter. Then I created a module and stored the file extension of the file in the extension field with hook_node_presave like this:

<?php
function MYMODULE_node_presave($node){
 
$file = file_load($node->field_MYFILEFIELD[LANGUAGE_NONE][0]['fid']);
 
$filename_array = explode('.', $file->filename);
 
$file_extension = array_pop($filename_array);
 
$node->field_extension[LANGUAGE_NONE][0]['value'] = $file_extension;
}
?>

Now views can use the "Extension" field just like any other. With sorting and filtering.

Hope that helps.

nobody click here