Adding an index to the portal catalog
Seems simple, right? Well, it is. Here's how!
THE PROBLEM:
I have an image field in my custom type. When accessing it in something like a folder listing, you don't actually have the object, but rather a brain. Therefore, you will not be able to test whether there is an image in this image field without waking up the object (bad). What I need is a catalog index I can look up to see if there is an image or not.
THE SOLUTION:
First, create a method in your class that returns what you want indexed. In my case, my method 'hasLogo' returns a boolean True or False based on what it finds in the image field. Your method can return whatever you need.
security.declarePublic('hasLogo')
def hasLogo(self):
""" This method returns true or false depending on whether
the object has an Image in the Logo field for the catalog """
result = 0
type = self['logo'].__class__.__name__
if type != 'str':
result = 1
return result
Then you add a few attributes to the field:
ImageField(
name='logo',
widget=ImageWidget(
label="Logo",
description="The logo",
),
storage=AttributeStorage(),
searchable=1,
sizes= {'large' : (768, 768),
'preview' : (400, 400),
'mini' : (200, 200),
'thumb' : (128, 128),
'tile' : (64, 64),
'icon' : (32, 32),
'listing' : (16, 16),
},
index="FieldIndex",
index_method="hasLogo"
),
The two attributes to note here are 'index' and 'index_method'. These can be added as Tagged Values if you're using ArchGenXML. These simple additions will give you what you need. Now there's one last thing that needs to be done to expose this index in the wy we need. Remember, we need this attribute to be exposed in a brain. Above we have only put this into the catalog. To get it in a brain, do the following:
index="FieldIndex:schema",
That tells the system to treat this as metadata and expose it in a brain. Now to use this in the page template. 'item' is a brain.
<tal:block tal:condition="item/hasLogo">
<img src="" tal:attributes="src string:${item_url}/logo_thumb" />
</tal:block>
Hope this helps you!
Further information
http://www.plope.com/Books/2_7Edition/SearchingZCatalog.stx#2-7
http://plone.org/documentation/manual/archetypes-developer-manual/fields/fields-reference
