Attention

This document was written for an unsupported version of Plone, Plone 2.5.x, and was last updated 898 days ago.

For more information, see the version support policy.

To learn how to upgrade to the current version of Plone, read the upgrade manual.

Reversing folder order to have newer items on top

by Sven Deichmann last modified Dec 06, 2009 09:28 PM
This will explain how you reverse the order of any folder object in Plone using getFolderContents to have newly added items automatically on top (which will be the natural order for all things like news, blogs and so on so that newer items are on top)

As Martin Aspeli stated out by comment on this:

BE WARNED: Doing this the way stated below will change the standard behaviour of all folders within the site and may break addon products

Look for an alternative way further down, if you only want this behaviour for special folders only


To change the order of folder contents, you only need to do 2 simple steps:


  • reverse the sort order of the script that fetches the folder contents
  • reverse the functionality of the "up" and "down" arrows

To reverse the sort order of the folder content itself, you only need to add a

    contentFilter['sort_order'] = 'reverse'

to getFolderContents.py (you find the Original in CMFPlone/skins/plone_scripts) so that it contains these lines:

if not contentFilter.get('sort_on', None):
    contentFilter['sort_on'] = 'getObjPositionInParent'
    contentFilter['sort_order'] = 'reverse'

It's up to you if you make a copy to one of your own products (where it can brake in further Plone versions), "customize" it in the ZMI (where even changes on your Products will be overridden) or edit it in place (where it is overridden on Plone updates).

To reverse the functionality of the "up" and "down" arrows, you need to edit folder_contents.pt so that it reads

<a href=""
title="Move item up"
i18n:attributes="title title_move_item_up;"
tal:attributes="href string:$here_url/folder_position?position=down&amp;id=$quoted_item_id&amp;template_id=${template_id}">
<img tal:replace="structure arrowUp" />
</a>
&nbsp;
<a href=""
   title="Move item down"
   i18n:attributes="title title_move_item_down;"
   tal:attributes="href string:$here_url/folder_position?position=up&amp;id=$quoted_item_id&amp;template_id=${template_id}">
  <img tal:replace="structure arrowDown" />
</a>

(The important thing is the position=down and position=up)

This seems not to interfere with the drag'n'drop re-ordering (hurra!)

Alternative way for individual folders only

(also usable as standard view of course, but only usable in Plone 2.1 and up)

As an alternative, make a copy of the standard folder listing template (or whatever kind of folder view you want to reverse) and use for example:

here.getFolderContents(contentFilter = {'sort_on' : 'Date', 'sort_order' : 'reverse' })

or

here.getFolderContents(contentFilter = {'sort_on' : 'getObjPositionInParent', 'sort_order' : 'reverse' })

instead of the standard here.getFolderContents()

The rest of the customization is like the one mentioned above for the template, no need to customize the getFolderContents script for that. The last step would be to register the new template as view for folders (you could simply add it to the list of templates under portal_types/Folder in the ZMI or seek further documentation on how to add a view to your own or the standard folder type)

By the way: this does not even interfere with the changes above, as they only fire without any contentFilter set. The first alternative only reverses the standard behaviour if no other behaviour was given by the calling template. Which can be good and bad: It should make interferences with other products or expected behaviour less a problem, but it also means that whenever you change the sort filter, you will need to reverse on your own. This could also be very confusing to people that are allowed to create new searches like smart folders or have access to the ZMI and do not know about the "natural" order Zope uses. (remember that the "real order" will remain the same as before with newer items on the bottom (if I find out who is responsible for that on a web project...))


Contribute

Something wrong or out of date? Anybody can edit or create a new article in the knowledge base. Simply create an account on this site, log in, and click the Edit button to contribute.