Personal tools
Home Webmaster Resources Zope/Plone Sorting by an objects folder position

Sorting by an objects folder position

Often times when querying the catalog to display information, it makes sense to sort the results based on the objects position in a folder. This can be accomplished in the following steps:
  1. Add a python script called folderPosition and insert the following code:

    try:
      return context.getObjectPosition(context.getId())
    except:
      return 0

  2. Inside portal_skins/plone_scripts, there is a python script called folder_position, customize the script and replace the code with the following:

    from Products.PythonScripts.standard import url_quote

    if position.lower()=='up':
      context.moveObjectsUp(id)

    if position.lower()=='down':
      context.moveObjectsDown(id)

    if position.lower()=='top':
      context.moveObjectsToTop(id)

    if position.lower()=='bottom':
      context.moveObjectsToBottom(id)

    objs = context.objectValues()
    for obj in objs:
      obj.reindexObject('folderPosition')

    if position.lower()=='ordered':
      context.orderObjects(id)

    msg="Item's position has changed."
    response=context.REQUEST.RESPONSE
    return response.redirect('%s/%s?portal_status_message=%s' % (context.absolute_url(), 'folder_contents', url_quote(msg)) )

  3. Add a folderPosition field index to the portal_catalog using the indexes tab.

Need assistance with your project? Universal Web Services can help.
Contact us to request a quote.