How can one make items/browse default to sorting by date?

I am using Omeka to index items by subject. More recent material on one's subject will most likely be more relevant, and I would like the items/browse page to default to displaying results by date starting with the newest.

I have looked at different posts (though the ones from a while back might be for an older version of Omeka). I see how to alter URLs to display results by date, but can you also alter the default sort on the items/browse page?

I looked at this from John Flatness on the dev group. I think it means that sorting would be controlled by using the sort_field and sort_dir parameters with this: item-search-filters. I would like an example of how to use that if someone is up for showing me -- or to be put on the right track if I am totally off too.

Are you refering to something entered into the Dublin Core Date field, or sorting by the most recently added item? Sorting by most recent first should be the default sort, so if that's not what's appearing something else is amiss.

The easiest way to change sorting, at least from the main navigation links, is to go to Appearance->Navigation and create a new link to replace the default link to items/browse. The URL would be something like

items/browse?sort_field=id&sort_dir=d

Thanks for responding. Yes, I wanted to sort by the Dublin Core Date field. I would like to be able to alter the default sorting on search results pages too -- not just when someone clicks the browse items link, but whenever the browse.php files is used. (Although it had never occurred to me that I could change the navigation links that way, and thanks for suggesting it.)

After reading the post in the dev group linked above, I am thinking that I need to alter this line in the theme's browse.php:
<?php echo item_search_filters(); ?>
I think that the sort_field and sort_dir parameters might be used to alter the default sorting, but I couldn't find an example of using those parameters and I don't know how to use them.

Can anyone show us code to make it sort by a Dublin Core metadata field?

Got it.

The item_search_filters() does something very different. If someone arrives at the browse page from doing a search on items, it shows what they searched for, e.g., what keyword or collection.

What you want would go in a plugin, and use the items_browse_params filter. (I notice some errors in the documentation there that I'm fixing now.)

All together it would be a plugin that looks like this:

<?php
class SortPlugin extends Omeka_Plugin_AbstractPlugin
{

    protected $_filters = array('items_browse_params');

    public function filterItemsBrowseParams($params)
    {
        //only apply to public side
        if (! is_admin_theme()) {
            //sort by date instead of order
            $params['sort_field'] = "Dublin Core,Date";
            $params['sort_dir'] = 'd';
        }
        return $params;
    }
}

Now, the huge caveat.

Dates are . . . special, no matter what. They're extra special in Omeka, because we store them as text strings. That means dates entered as "June 5, 2014" and "May 4, 2929" will sort differently from dates stored as "2014-07-05" and "2929-05-04". Thus, depending on how they are formatted, they might or might not sort as expected

Wow -- that was more complicated than I had thought. Thank you so much for the explanation and the code.
I didn't know how to make a plugin, but this page suggested that I look at the Simple Pages plugin to learn. I used that as a model, and got rid of everything but configuration file and 1 PHP file and put your code in the PHP file. Now I have a little plugin that will sort things by the identifier field.
Thanks for that advice on the date field too -- I was using the identifier field to list each item's citation, and that was easier for me to use to sort than the date because of how the items were cited.

Can you share that plugin? I'd like to have my collection sorted by item date by default.

I can -- but it might not be what you expect. I wanted to get it to sort by a Dublin Core field as the default, but now it only sorts by that field -- so users can't change the sort order. (I ended up removing those links at the top that would allow for users to change the sort order because they no longer did anything.)
I read about it, and there seems to be a way to make a new default order and still let users adjust the order, but I didn't know how to do that.
If you still want it, then I will absolutely post it -- though it is just what patrickmj posted above with "Identifier" put in where "Date" is now and dropped inside a plugin.

Hi,

You can check the sort in the filter:

/**
* Filter items browse params in order to set the default order to Date.
*
* @param array $params
* @return array
*/
public function filterItemsBrowseParams($params)
{
// Only apply to public side.
if (!is_admin_theme()) {
// Only apply to collections/show.
$requestParams = Zend_Controller_Front::getInstance()->getRequest()->getParams();
if (!($requestParams['controller'] == 'collections' && $requestParams['action'] == 'show')) {
// If no sort, sort by Dublin Core Date, ascendant.
if (!isset($params['sort_field'])
|| empty($params['sort_field'])
|| $params['sort_field'] == 'added'
) {
$params['sort_field'] = 'Dublin Core,Date';
$params['sort_dir'] = 'a';
}
}
}

return $params;
}

The standard Dublin Core recommends that dates be formatted (2014-06-20). You can add another filter to display dates as you want (language dependent).

Sincerely,

Daniel Berthereau
Infodoc & Knowledge management

Question to this: I implemented the sort functionality Patrick outlined via a plugin. While it is sorting correctly, the display class sorting is still added to Date added.

I stepped through the code, and $current_sort = trim($req->getParam($sortParam)); located in browse_sort_links() still gets set to added, resulting in the class being added to the wrong item. How can than be changed? I initially assumed this was because the browse_sort_links gets called before filterItemBrowseParams in the plugin, but that is not the case.

It looks like in browse_sort_links() the $req object is the http request, so it's basically looking at the GET param for the sort info.

You could try to mimic it in the hook like this:

$sortParam = Omeka_Db_Table::SORT_PARAM;
$req = Zend_Controller_Front::getInstance()->getRequest();
$param = $req->getParam($sortParam);
if($param == 'added') {
$req->setParam($sortParam, 'Dublin Core,Date');
}

Not sure if that'd work, and I'd worry about unexpected consequences elsewhere, but it might do the job.

It works indeed - thanks. I also incorporated the sortDirParam. Thanks

I have created a small plugin which updates the default sort : https://github.com/anuragji/DefaultSort

Thanks anuragji !
Your plugin does exactly what I needed.

Glad to hear that you found it useful.

I tried the plugin, and the browse/items sorted for me, but not the browse/collections.
Would you take a looks at the collections part of it please in case it's not just me? It looks like it is referring to items instead of collections in some spots and uses both the plural and singular of collection in places -- that's as far as I got, and I couldn't alter it to work.
I went back to the code before that will sort for both, but will not let the use alter the sort order, which is less nice than what you have.
If you are not up for looking at it again -- no worries. It works great for items and was good to see that done.

Dear reo,

I've updated the code, which indeed contained a bug. It should be working now.

Thanks!

Anurag