- First, I create a vocab.py in the package's root, alongside configure.zcml, then put my vocabularies there:
from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm def MyVocabulary(context): items = [ ("value1", u"This is label for item"), ("value2", u"This is label for value 2")] terms = [ SimpleTerm(value=pair[0], token=pair[0], title=pair[1]) for pair in items ] return SimpleVocabulary(terms)
A 'titled' vocabulary like this one will, for example, allow you to use a UID as a form's returned value but display a human-readable title by a checkbox.
I've generally found it easiest to use the SimpleVocab classes. - This needs registering as a named IVocabularyFactory utility in configure.zcml:
<utility provides="zope.schema.interfaces.IVocabularyFactory" component=".vocab.MyVocabulary" name="MyVocabularyName" /> - Then you can use this in a schema:
class MyWidgetSchema(interface.Interface): categories = schema.Set( value_type = schema.Choice( vocabulary = u"MyVocabularyName", title = u"Favourite Things"), default = set(), title = u"Favourite Things" )oratapi.StringField('item_favourite', required = 1, vocabulary_factory = u"MyVocabularyName", widget = atapi.SelectionWidget( label = _('Favourite Things'), description = _('Favourite Things'), ), ),
Wednesday, January 18, 2012
Plone 4: Adding vocabulary to a Content Type field (Archetype)
A while back, I needed to use vocabularies with Archetypes. Being still new in Plone, I rummaged the internet for a while. This is the way I did it on Plone 4.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment