I have a view of file entities, sorted by upload date. When I edit one of the fields attached to one of my files the upload date of the file gets updated. The view now shows the last modified file first and no longer the last uploaded. Is this the normal behavior? Is there a way to actually sort my view by upload date?

1 Answer 1


A quick check with drupal_get_schema() confirms that file entities have only one associated time column called timestamp; there is no separate "upload" and "modified" date. So that timestamp, presumably, gets updated every time the file entity gets saved.

This makes sense from a certain standpoint because editing a file entity could involve updating the file itself.

I decided to take a crack at a custom module that would add a new "upload date" column to the file entity table. This may be completely wrong, and I haven't tested it, so do NOT try to use it on a production website until you're sure it works, but it might just give you what you're looking for.

In mymodule.install:

function mymodule_install() {
  // Add a column to the file table for your new creation date field.
  db_add_field('file_managed', 'upload_date', array(
    'type' => 'int',
    'unsigned' => TRUE,
    'not null' => TRUE,
    'default' => 0
  // Load all existing files and set their upload date to be equal to their current timestamp.
  $files = entity_load($file);
  foreach($files as $file) {
    $file->upload_date = $file->timestamp;
// Drop the new field if the module is uninstalled.
function mymodule_uninstall() {
  db_drop_field('file_managed', 'upload_date');

In mymodule.module:

// Set the upload time field to the current time when the file is first saved
function mymodule_file_presave($file) {
  if(!isset($file->upload_date) || $file->upload_date == 0) {
    $file->created_date = time();
// @TODO make the upload date field editable.

In mymodule.views.inc:

// Add a field handler for your new field.
function mymodule_views_data_alter(&$data) {
  $data['file']['upload_date'] = array(
    'title' => t("Upload date"),
    'help'  => t("The date the file was first uploaded."),
    'field' => array(
      'handler' => 'views_handler_field_date',

Assuming that I didn't screw up, once the module is installed you'll need to add the upload date field to your view and use it for sorting.

  • Thanks for the info! Is there an advantage of this approach over just adding another field 'field_upload_time' and populating it upon upload but not making it accessible afterwards? I could then certainly sort the view by this new field.
    – deinqwertz
    Aug 7, 2013 at 21:14
  • @deinqwertz You know, probably not, apart from the automation, which you could potentially do with a field anyway. It's probably slightly more lightweight than a field and it brings the file entity schema more inline with that of other entities, like nodes, that have a separate "created" and "changed" column out-of-the-box, but as for being simplest to implement, you have me beat with the obvious.
    – Eric N
    Aug 7, 2013 at 21:23

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.