zopefoundation / z3c.form

An advanced form and widget framework for Zope 3
Other
9 stars 39 forks source link

wrong context for vocabulary in widget #77

Open agschaid opened 6 years ago

agschaid commented 6 years ago

Hi there,

I have a usecase where I need a relation field that only presents a narrowly subset of elements as possible related items to the user. The selection of possible elements is determined by the current element (the one that we are currently editing).

Please consider this stub version of a vocabulary:

from plone.app.vocabularies.catalog import CatalogVocabulary
from plone.memoize.instance import memoize

class SiblingVocabulary(CatalogVocabulary):

    def __init__(self, context):
        self.context = context

    def __contains__(self, value):
        print "contains"
        return True # this is ok ;)

    @property
    @memoize
    def brains(self):
        # this overrides the property in CatalogVocabulary
        print "brains", self.context
        return [] # this is where I will compute the valid elements, using the context

it is used by this factory:

from zope.interface import implementer
from zope.schema.interfaces import IVocabularyFactory

@implementer(IVocabularyFactory)
class SiblingVocabularyFactory(object):
    def __call__(self, context, query=None):
        print "Factory", context
        return SiblingVocabulary(context)

which is configured like this in the config.zcml

<utility
    factory=".languagesibling.SiblingVocabularyFactory"
    name="poi.dwa.sibling_vocabulary"
/>

The widget/field is configured like this:

@provider(IFormFieldProvider)
class ILanguageSibling(Schema):
    explicit_siblings = List(
        title=_(u'languagesibling.explicit_siblings.title', default=u"Select explicit siblings"),
        required=False,
        value_type=RelationChoice(
            vocabulary='poi.dwa.sibling_vocabulary'
        )
    )

    widget(
        EXPLICIT_SIBLINGS_FIELD,
        RelatedItemsFieldWidget,
        orderable=True,
        pattern_options={
            'scanSelection': True,
            'mode': 'search',
            #'currentPath': current_path,
            #'basePath': base_path,
        }
    )

Now. If I open the edit mode I get this printout: Factory <Folder expected-element>

But when I click the widget in order to display all valid elements I get this:

Factory <Folder at expected-element>
Factory <PloneSite at Plone>
brains <PloneSite at Plone>

This corresponds to these two requests I see in my browser:

http://localhost:8080/Plone/at/campaigns/expected-element/@@edit/@@z3cform_validate_field
http://localhost:8080/Plone/@@getVocabulary?name=poi.dwa.sibling_vocabulary&field=explicit_siblings&query=...

Note: the target of the getVocabulary request is my site-root (thus I do not get the expected context in my vocabulary factory) and not the expected-element.

Am I right to assume that this is a bug?