2014/07/30

Collection Group By View

A Collection in Plone works much like a report or query does in a database. Based on a set of Criteria such as: content types, dates, or keywords, you can search items and display them in a variety of dynamic ways. By default, there are Standard View, Summary View, Full View, Tabular View and Thumbnail View. What I need is to add an Aggregate View (like Group By). Here is how:

First, create a view.py file in the browser folder, where you define the View class. In my case, the results need to be grouped by category field values. Now I get a dictionary like {'category1': [<plone.app.contentlisting.catalog.CatalogContentListingObject instance at /mysite/myfolder/101-c-1>, <plone.app.contentlisting.catalog.CatalogContentListingObject instance at /mysite/myfolder/101-c-2>, <plone.app.contentlisting.catalog.CatalogContentListingObject instance at /mysite/myfolder/101-c-3>], 'category2': [...]}. With TAL nested loop in the aggregate_view.pt template, we can display the result. Finally register this browser view.

Note that this tip might suit only for collections of limited items. There should be much room for performance improvement. For those who are interested in the details how Collection works, see reference at plone.app.contenttypes/behaviors/collection.py