Search code examples
plonedexterity

Compute Description Based on Specific Field


My custom dexterity type looks like this:

class IMyType(form.Schema):
    title = schema.TextLine(
        title=_(u"Title"),
    )
    address = schema.TextLine(
        title=_(u"Address"),
        required=False,
    )

class MyType(dexterity.Container):
    grok.implements(IMyType)

I want the Live Search result looks like this, listing its title and first 3 char of the address value if existing:

Item One
  Address[:3]

Item Two
  Address[:3]

By default, each item matched will show its title and description. Thus, one solution is making the description field computed from the address field. But I don't know how. Any hint or better suggestion?


Solution

  • You could override the Description index for your specific type using plone.indexer. This way the catalog has the right information and you don't have to customize the search result.

    from plone.indexer import indexer
    
    @indexer(IMyType)
    def custom_description(obj, **kw):
        return obj.Description[:3]
    

    Register

    <adapter name="description" factory=".indexers.custom_description" />
    

    Check plone.indexer documentation on pypi https://pypi.python.org/pypi/plone.indexer