Archive for September, 2008|Monthly archive page

Stuff the catalog

Adding extra information to the portal catalog can be an easy way to get information from across your site. Plone let’s you easily create extra indexes using a generic setup profile. If you have an existing profile you can add a file catalog.xml with the following contents:

<?xml version=”1.0″?>
<object name=”portal_catalog” meta_type=”Plone Catalog Tool”>
<column value=”FirstLetter”/>
<index name=”FirstLetter” meta_type=”FieldIndex”>
<indexed_attr value=”FirstLetter”/>
</index>
</object>
This will create both a field index named FirstLetter and a corresponding meta data column (this will be available on the result objects when querying the catalog). There are many kind of indexes you can create this way. You can see how to use some of them in Plone’s own catalog.xml file.

Now we need to make sure we that our FirstLetter index will be filled. We can do this by creating a simple Python function. Let’s create a new file for this in our project named catalog.py with this content:

from Products.CMFPlone.CatalogTool import registerIndexableAttribute

def first_letter(obj, portal, vars, **kwargs):
     # Make sure we have a title with something in it
    if obj.Title():
        # Return the first letter
        return obj.Title()[0].lower()

registerIndexableAttribute('FirstLetter', first_letter)

This will register an extra handler for Plone’s indexing system. To make sure it is registered we need to import the catalog module from the __init__.py of the project/product. Just add a line like this to your project (replace exampleproject with your project’s name):

import exampleproject.catalog

This should make the FirstName index available when the project is installed. Now you query your catalog against this with something like:

portal_catalog(FirstLetter='a')

Advertisements