Custom Object Source Binder for z3c.forms

Within a folderish dexterity content type i had to select objects for a relation field from just that context. Therefore i wrote a small Custom Source Binder.

from plone.formwidget.contenttree.source import ObjPathSource
from plone.formwidget.contenttree.source import ObjPathSourceBinder


class SubTreeObjPathSource(ObjPathSource):

    def __init__(self, context, selectable_filter, 
                 navigation_tree_query=None):

        super(SubTreeObjPathSource, self).__init__(
                context, selectable_filter, 
                navigation_tree_query=navigation_tree_query)

        del self.navigation_tree_query['portal_type']
        self.navigation_tree_query['path']['query'] = \
                '/'.join(context.getPhysicalPath())


class SubTreeObjPathSourceBinder(ObjPathSourceBinder):
    path_source = SubTreeObjPathSource

 

And the updated field definition: 

    contact_person = RelationList(
        title = _(u"Contact Person"),
        value_type = RelationChoice(
            source = SubTreeObjPathSourceBinder()
        )
    )