Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reverse the Plone default sort order in folder_contents

How can i make my folderish custom type display it's object listing in reverse chronological order in folder_contents view?

Default is oldest object at the top of the list, I would like a new object just added to be at the top of the list.

Would be nice if Plone had this feature out of the box... if it does i can't find it.

like image 390
Aaron Williams Avatar asked Dec 04 '22 02:12

Aaron Williams


1 Answers

To actually change the obj-position in its parent you can utilize Zope2's OFS.IOrderedContainer-interface for accessing the relevant methods and hook it up on zope.lifecycleevent.interfaces.IObjectAddedEvent, like in this Plone-addon "adi.revertorder" (disclaimer: author=meh):

In you configure.zcml register the eventlistener:

<subscriber for="Products.CMFCore.interfaces.IContentish
                 zope.lifecycleevent.interfaces.IObjectAddedEvent"
            handler=".subscriber.revertOrder" />

And in the handler (here: subscriber.py), define the called method:

from Acquisition import aq_inner
from Acquisition import aq_parent

from OFS.interfaces import IOrderedContainer

def revertOrder(obj, eve):
    """
    Use IOrderedContainer interface to move an object to top of folder on creation.
    """
    parent = obj.aq_inner.aq_parent
    ordered = IOrderedContainer(parent, None)
    if ordered is not None:
        ordered.moveObjectToPosition(obj.getId(), 0)

Applies to Dexterity- and Archetype-based contenttypes.

See also the docs: http://docs.plone.org/external/plone.app.dexterity/docs/advanced/event-handlers.html

like image 128
Ida Avatar answered Jan 11 '23 14:01

Ida