0

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?

marr
  • 861
  • 4
  • 19
  • Hi Marr, could one of the answers help/do you need more info? – Ida Dec 04 '13 at 15:22
  • These answers are appreciated and of great help. I probably needs them both in my case. Will find time to try them out. – marr Dec 05 '13 at 01:26
  • 2
    I'd say it depends on your usecase. If you want the address to behave in general/globally like the description: go for mat's proposal. Though that assumes, you don't need a description for your contype at all, and then you could use the desc-field for adress-input anyway. If you only need this in the livesearch-results, go for customizing livesearch_reply. – Ida Dec 05 '13 at 07:42
  • Nice. Your comment deserves the best answer. – marr Dec 06 '13 at 01:48
  • :D how sweet to diplomatically 'divide' possible points to give, to the two answers and glad you have all the infos now, you need! – Ida Dec 07 '13 at 08:20

2 Answers2

1

Quoting http://somedoma.in:8080/somePloneSiteId/portal_catalog/manage_catalogSchema:

"It is important to understand that when the Catalog is searched, it returns a list of result objects, not the cataloged objects themselves, so if you want to use the value of an object's attribute in the result of a search, that attribute must be in this list"

So, after adding your fieldname to the metadata-index, you can customize livesearch_reply, to achieve what you want, insert after line 52 (Products.CMFPlone-4.3) where "display_description" is set, this:

if result.portal_type == 'yourtype':
    display_description = safe_unicode(result.address)
Ida
  • 3,994
  • 21
  • 40
0

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

Mathias
  • 6,777
  • 2
  • 20
  • 32