Skip to content

Commit

Permalink
The conditional fields have now widgets that can be customized as needed
Browse files Browse the repository at this point in the history
  • Loading branch information
ale-rt committed Sep 9, 2024
1 parent 7e4dfb8 commit 8bb10f7
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 2 deletions.
5 changes: 4 additions & 1 deletion docs/changes.rst
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
Changelog
=========

16.2.2 (unreleased)
16.3.0 (unreleased)
-------------------

- The conditional fields have now widgets that can be customized as needed
[ale-rt]

- Update prototype from commit 317e62820b35678055240ca911d24a3b45db44d6
Ref: scrum-2333
[reinhardt]
Expand Down
2 changes: 2 additions & 0 deletions src/euphorie/content/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@
/>

<adapter factory=".dependency.ConditionalFieldValidator" />
<adapter factory=".dependency.ConditionalTextFieldWidget" />
<adapter factory=".dependency.ConditionalHtmlTextFieldWidget" />

<adapter
factory=".help.SearchableTextIndexer"
Expand Down
68 changes: 67 additions & 1 deletion src/euphorie/content/dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,93 @@

from euphorie.content.user import BaseValidator
from euphorie.htmllaundry.z3cform import HtmlText
from euphorie.htmllaundry.z3cform import IHtmlText
from plonetheme.nuplone.z3cform.directives import Dependency
from plonetheme.nuplone.z3cform.interfaces import INuPloneFormLayer
from z3c.form.browser.text import TextWidget
from z3c.form.browser.textarea import TextAreaWidget
from z3c.form.interfaces import IFieldWidget
from z3c.form.interfaces import IForm
from z3c.form.interfaces import ITextAreaWidget
from z3c.form.interfaces import ITextWidget
from z3c.form.interfaces import IValidator
from z3c.form.widget import FieldWidget
from zope import schema
from zope.component import adapter
from zope.interface import implementer
from zope.interface import implementer_only
from zope.interface import Interface
from zope.schema.interfaces import IFromUnicode


class ConditionalField:
"""Marker class for conditional fields."""
"""Marker class for conditional fields.
FIXME: this class is used to register a custom validator adapter
for the copnditional text input and textarea fields.
We should avoid using this mixin class and
use a proper common interface instead.
"""


# Interfaces
class IConditionalTextLine(IFromUnicode):
"""A Text line that is only shown under certain conditions."""


class IConditionalHtmlText(IHtmlText):
"""HTML Text field that is only shown under certain conditions."""


class IConditionalTextWidget(ITextWidget):
"""Text widget that is only shown under certain conditions."""


class IConditionalHtmlTextWidget(ITextAreaWidget):
"""Text area widget that is only shown under certain conditions."""


# Fields
@implementer(IConditionalTextLine)
class ConditionalTextLine(schema.TextLine, ConditionalField):
"""A Text line that is only shown under certain conditions."""


@implementer(IConditionalHtmlText)
class ConditionalHtmlText(HtmlText, ConditionalField):
"""HTML Text field that is only shown under certain conditions."""


# Widgets
@implementer_only(IConditionalTextWidget)
class ConditionalTextWidget(TextWidget):
"""Text widget that is only shown under certain conditions."""

klass = "conditional-text-widget"


@implementer_only(IConditionalHtmlTextWidget)
class ConditionalHtmlTextWidget(TextAreaWidget):
"""Text area widget that is only shown under certain conditions."""

klass = "conditional-textarea-widget"


# Field widget factories
@adapter(IConditionalTextLine, INuPloneFormLayer)
@implementer(IFieldWidget)
def ConditionalTextFieldWidget(field, request):
return FieldWidget(field, ConditionalTextWidget(request))


@adapter(IConditionalHtmlText, INuPloneFormLayer)
@implementer(IFieldWidget)
def ConditionalHtmlTextFieldWidget(field, request):
return FieldWidget(field, ConditionalHtmlText(request))


# Validator
@implementer(IValidator)
@adapter(Interface, Interface, IForm, ConditionalField, Interface)
class ConditionalFieldValidator(BaseValidator):
Expand Down

0 comments on commit 8bb10f7

Please sign in to comment.