Question

I have a Plone (4.2) form that is grokked. I want to have a dynamic source for a specific field. The relevant part of the schema:

from plone.directives import form
from z3c.relationfield.schema import RelationList, RelationChoice
from five import grok
from plone.formwidget.contenttree import ObjPathSourceBinder


@grok.provider(ISourceContextBinder)
def availableAttachments(context)
    return ObjPathSourceBinder()


class IEmailFormSchema(form.Schema):

    attachments = RelationList(
        title = _(u'Attachments'),
        description = _(u'Select and upload attachments.'),
        default = [],
        value_type = RelationChoice(
                    title =_(u"attachment"),
                    default = [],
                    # source = ObjPathSourceBinder() # this works
                    source = availableAttachments),  # should do the same, but doesn't

        required = False
    )

this leads to:

ValueError: Invalid clone vocabulary

I tried every variant that is described in the plone dexterity developer manual. A method with decorator in combination with the source attribute of RelationChoice (see above) and a named Vocabulary class both with the same result.

Was it helpful?

Solution

Instead of calling for help, I should have called the ObjPathSourceBinder object. This code works as expected:

@grok.provider(IContextSourceBinder)
def availableAttachments(context):

    path = '/'.join(context.getTmp_folder().getPhysicalPath())
    query = { "portal_type" : ("File","Image"),
              "path": {'query' :path } 
             }

    return ObjPathSourceBinder(navigation_tree_query = query).__call__(context) 

in combination with the schema code from my question.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top