* Dan Korostelev <nad...@gmail.com> [2009-02-09 19:08]:
> Source support - Seems to work fine. I've checked that out in my
> sandbox instance with zc.sourcefactory's context-less and
> context-based sources.

I'd very much like to put in a little bit of flexibility when looking up widgets
for source-based fields: zope.formlib looks up the widget for an IChoice field
by requiring a multi-adapter for (field, field.source) instead of just the
field, thus allowing you to differentiate widgets for different kinds of
sources.

I'd like to introduce this to z3c.form as well (see attached patch). Would it be
alright with you for me to commit this to trunk (to then go into the release)?

Wolfgang

-- 
Wolfgang Schnerring · w...@gocept.com
gocept gmbh & co. kg · forsterstraße 29 · 06112 halle (saale) · germany
http://gocept.com · tel +49 345 1229889 0 · fax +49 345 1229889 1
Zope and Plone consulting and development

Index: src/z3c/form/browser/select.py
===================================================================
--- src/z3c/form/browser/select.py	(revision 96293)
+++ src/z3c/form/browser/select.py	(working copy)
@@ -75,7 +75,18 @@
 
 @zope.component.adapter(zope.schema.interfaces.IChoice, interfaces.IFormLayer)
 @zope.interface.implementer(interfaces.IFieldWidget)
-def SelectFieldWidget(field, request):
+def ChoiceWidgetDispatcher(field, request):
+    """Dispatch widget for IChoice based also on its source."""
+    return zope.component.getMultiAdapter((field, field.vocabulary, request),
+                                          interfaces.IFieldWidget)
+
+
+# can change to ISource once vocabularies are deprecated
+...@zope.component.adapter(zope.schema.interfaces.IChoice,
+                        zope.schema.interfaces.IBaseVocabulary,
+                        interfaces.IFormLayer)
+...@zope.interface.implementer(interfaces.IFieldWidget)
+def SelectFieldWidget(field, source, request):
     """IFieldWidget factory for SelectWidget."""
     return FieldWidget(field, SelectWidget(request))
 
@@ -98,4 +109,4 @@
 @zope.interface.implementer(interfaces.IFieldWidget)
 def CollectionChoiceSelectFieldWidget(field, value_type, request):
     """IFieldWidget factory for SelectWidget."""
-    return SelectFieldWidget(field, request)
+    return SelectFieldWidget(field, None, request)
Index: src/z3c/form/browser/select.zcml
===================================================================
--- src/z3c/form/browser/select.zcml	(revision 96293)
+++ src/z3c/form/browser/select.zcml	(working copy)
@@ -11,6 +11,10 @@
   </class>
 
   <adapter
+      factory=".select.ChoiceWidgetDispatcher"
+      />
+
+  <adapter
       factory=".select.SelectFieldWidget"
       />
 
Index: src/z3c/form/testing.py
===================================================================
--- src/z3c/form/testing.py	(revision 96293)
+++ src/z3c/form/testing.py	(working copy)
@@ -281,6 +281,7 @@
         IPageTemplate, name=interfaces.DISPLAY_MODE)
 
     # Select Widget
+    zope.component.provideAdapter(select.ChoiceWidgetDispatcher)
     zope.component.provideAdapter(select.SelectFieldWidget)
     zope.component.provideAdapter(
         widget.WidgetTemplateFactory(getPath('select_input.pt'), 'text/html'),
_______________________________________________
Zope-Dev maillist  -  Zope-Dev@zope.org
http://mail.zope.org/mailman/listinfo/zope-dev
**  No cross posts or HTML encoding!  **
(Related lists - 
 http://mail.zope.org/mailman/listinfo/zope-announce
 http://mail.zope.org/mailman/listinfo/zope )

Reply via email to