SilverStripe’s File, Folder and Image objects have a default sort of ‘Name’, ‘Sort’ and ‘Name’ respectively, but if you are also using Uncle Cheese’s DataObjectManager the sort order will change to the ‘SortOrder’ field. Unless you’ve been sorting these yourself they’re going to appear in the ImageField control in a more or less random order, which isn’t idea. This post shows the solution.
SilverStripe version used
I have used the methods succesfully in this post with SilverStripe 2.4.5
Changing the default_sort for ImageField and FileField
In your mysite/_config.php file, add the following:
File::$default_sort = 'Name';
Folder::$default_sort = 'Name';
Image::$default_sort = 'Name';
Alternatively make Folder::$default_sort = ‘Sort’ to restore the default behaviour, but I personally prefer to keep it in name order.
Files and Images table
Changing the default_sort as shown above won’t affect the order for the table of files/images in the Files and Images section of the CMS. However the DataObjectManager allows you to change the sort order by clicking the column headings, so it’s not really an issue so much.
However, if you want to make it use the default sort instead, the only way I could work out how to do this myself is to remove the File class from the sortable classes.
In theory, you should be able to do this:
SortableDataObject::remove_sortable_class('File');
But it didn’t work when I tried it because it only removes the extension from the class and doesn’t remove it from the SortableDataObject::$sortable_classes array. You could do this instead, although it’s a but hacky and may have unexpected consequences.
Again, add this to your mysite/_config.php file:
SortableDataObject::$sortable_classes = array();
and then move your existing SortableDataObject::add_sortable_classes declaration below it, if you did already have one.
This will not only make the table now sort in the default_sort, but will remove the useful features of dataobject_manager from the table, so I’d generally advise not to do this; I’m simply showing that it can be done.