It looks now like this:
from Products.CMFCore.utils import getToolByName
from zope.interface import provider
from zope.schema.interfaces import IContextSourceBinder
from zope.schema.vocabulary import SimpleVocabulary
from plone.dexterity import utils
from my.plone.addon.interfaces import IMyResource
@provider(IContextSourceBinder)
def registeredResources(context):
typeList = getToolByName(context, 'portal_types').listTypeInfo()
terms = []
for type in typeList:
dottedName = getattr(type, 'klass', None)
if dottedName is None:
continue
if not IMyResource.implementedBy(utils.resolveDottedName(dottedName)):
continue
terms.append(
SimpleVocabulary.createTerm(
type.id,
type.title,
type.description
)
)
return SimpleVocabulary(terms)
What do you think? I don't like the utils.resolveDottedName
very much but I found no other way around it. Seems slow to me. On the other side there are never as much types registered and in the end it runs in O(n). Should be fine.
I may could use filter()
, map()
or a generator but I don't know if this would make things much faster.