Sorting files on panel

How can i sort files on creation or modified date on Panel? I dont want sort manual with sortable: true, this should be default.

Tried following:

files: 
    num:
        mode: date
        field: modified
        format: YmdHi

This is not a valid option, see file settings in blueprints http://getkirby.com/docs/panel/blueprints/file-settings.

I know but there is no option about sorting here.

is there any solution to sort files by modfied though???

You should be able to sort by the time the files on disk have been last modified using this code snippet:

$page->images()->sortBy('modified', 'desc');

Let me know if that doesn’t work for you.

There is currently no way to show the files in the panel sorted by modified. It should be possible, however, to use a hook that changes the sorting number depending on modified date, on panel.file.upload or panel.file.update.

1 Like

I know this thread is a few months old, but I also wanted to sort files by date in Panel. So, based on @texnixe’s suggestion to use hooks, here is a solution:

<?php
//Save to site/plugins/file-sorting-by-date.php

kirby()->hook('panel.file.upload', 'uploaddatesort');
kirby()->hook('panel.file.replace', 'sortfiles');

function setdatetime($file) {

  //Reference Date Options:
  //"now": Current date and time.
  //"modified": Time of last file modification. If uploaded via Panel, this will be the upload date.
  //"taken": If a photo, then when it was taken. Falls back on "modified" behavior otherwise.
  $referencedate = "modified";

  switch ($referencedate) {
    case "now":
      $filedate = time('Y-m-d');
      $filetime = time('H:i');
      break;
    case "modified":
      $filedate = $file->modified('Y-m-d');
      $filetime = $file->modified('H:i');
      break;
    case "taken":
      if ($file->type() == "image"){
          $filedate = date('Y-m-d',$file->exif()->timestamp());
          $filetime = date('H:i',$file->exif()->timestamp());
      } else {
        $filedate = $file->modified('Y-m-d');
        $filetime = $file->modified('H:i');
      }
      break;
  }

  $file->update(array(
    'date' => $filedate,
    'time' => $filetime
  ));

}


function uploaddatesort ($file) {
  setdatetime($file);
  sortfiles($file);
}


function sortfiles($file) {
  //Order Options:
  //'asc': ascending, oldest to newest
  //'desc': descending, newest to oldest
  $order = 'desc';

  foreach ($file->files() as $f) {
    if ($f->date() == "") {
      setdatetime($f);
    }
  }

  $i = 1;
  foreach ($file->files()->sortBy('date', $order, 'time', $order) as $f) {
    $f->update(array(
      'sort'    => $i
    ));
    $i++;
  }
}

?>

Save that code in site/plugins/file-sorting-by-date.php, and in your blueprint enable files to be sortable with sortable: true. This will automatically sort the files by date whenever a file is uploaded or replaced. You can even choose to sort in ascending or descending order. To warn you, though, uploading files through Panel sets their modification date and time to the current date and time. Adding files manually to the page folder preserves their original modification date, and then you will either need to upload one more file or replace one of the manually added files via the panel to activate the sorting script.

Edit: Added sorting by time as well as date.

3 Likes