Skip to content

Instantly share code, notes, and snippets.

@pysailor
Last active August 29, 2015 14:03
Show Gist options
  • Save pysailor/d2cfa99c6d7e2e63fe0c to your computer and use it in GitHub Desktop.
Save pysailor/d2cfa99c6d7e2e63fe0c to your computer and use it in GitHub Desktop.
Expose a vocabulary from ATVocabularyManager to be used with dexterity content types
"""
This example assumes you have a simple (flat) vocabulay named `available-languages`
in portal_vocabularies
"""
# This would be your vocabularies.py
from Products.CMFCore.utils import getToolByName
from zope.interface import implementer
from zope.schema.interfaces import IVocabularyFactory
from zope.schema.vocabulary import SimpleTerm
from zope.schema.vocabulary import SimpleVocabulary
@implementer(IVocabularyFactory)
class AvailableLanguages(object):
def __call__(self, context):
atvm = getToolByName(context, 'portal_vocabularies')
available_languages = atvm.getVocabularyByName('available-languages')
terms = []
for term in available_languages:
terms.append(SimpleVocabulary.createTerm(
term, term.encode('utf-8'), available_languages[term].title))
return SimpleVocabulary(terms)
# In your configure.zcml, register the vocabulary as global utility:
<utility name="my.product.available-languagess"
factory=".vocabularies.AvailableLanguages" />
# Then, in your dexterity schema, you can reference this vocabulary
from zope import schema
available_languages = schema.Set(
title=_("Available language(s)"),
required=False,
value_type=schema.Choice(
vocabulary="my.product.available-languages",
),
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment