Adding conditions for Contentrules [SOLVED, kind of]

Is there any docs on 'adding conditions for content rules' (suggestions on 'what to google welcome'.

I made a content rule for 'Tags' (Subject), but it would be nice if it was possible to 'see other conditions'.

In other words:

  1. It is possible to have a conditon based on another condition: 'Content Type' == 'SomeType'
  2. It would be nice if I could have a condtion 'field'-value = 'something showing just the 'fields' that this content type has'
  3. For this, it would be best that 'my content condition' checked which fields 'SomeType' have, and then have one drop down with 'field' and one with 'value' (might require to find which widget is used for that field in the content type.

Is this doable ? Can 'condition2 find out if another condition is "Content Type' (and Page)

I think it is here. Conditions form are basically forms and apply the same patterns you've in Easyform or Dexterity other Plone forms...

Conditions are computed with the and logic, so you can filter over and over. This does not cover all the use cases, but adding another content rule can cover some "OR" cases.

Try also to see how zcml directive plone:ruleCondition works, maybe there's something useful.

Does this mean that 'condition 2' can 'know' settings for 'condition 1' (also) on Add and Edit view (or does this work only when 'excecuted' ?

Work only when executed. You can access the configs but support this use case is difficult.

My suggestion is to define your own condition that can have various inputs and act accordingly to your own logic.

Thanks (having the same as collection/querystring would be the dream), guess I need to make a new condtion that let one select first 'content type' and then 'fields' or just hard code field names for my use case.

For reference, a condition that checks 'somefield' for 'some value' can (kind of) be made with code below:
Since the value can be string or int or bool etc, I converted the value to the type of the field, which does does fell 'like the right way to do it', but will proably work 'for much':


from OFS.SimpleItem import SimpleItem
from plone.app.contentrules import PloneMessageFactory as _
from plone.app.contentrules.browser.formhelper import AddForm
from plone.app.contentrules.browser.formhelper import ContentRuleFormWrapper
from plone.app.contentrules.browser.formhelper import EditForm
from plone.contentrules.rule.interfaces import IExecutable
from plone.contentrules.rule.interfaces import IRuleElementData
from Products.CMFCore.Expression import createExprContext
from Products.CMFCore.Expression import Expression
from Products.CMFCore.utils import getToolByName
from z3c.form import form
from zope import schema
from plone.autoform import directives 
from zope.component import adapter
from zope.interface import implementer
from zope.interface import Interface



class IFieldCondition(Interface):
    """Interface for the configurable aspects of a Field condition.

    This is also used to create add and edit forms, below.
    """
    
    field = schema.TextLine(
        title=_("label_field", default="Field"),
        description=_(
            "help_field",
            default="Name of field",
        ),
        required=True,
    )
    
    value = schema.TextLine(
        title=_("label_field_value", default="Field Value"),
        description=_(
            "help_field_value",
            default="Field value",
        ),
        required=True,
    )
    

@implementer(IFieldCondition, IRuleElementData)
class FieldCondition(SimpleItem):
    """The actual persistent implementation of the FieldS condition
    element.
    """

    field = ""
    element = "plone.conditions.Field"

    @property
    def summary(self):
        return _(
            "Field ${field} is: ${value}",
            mapping={"field": self.field, "value": self.value},
        )


@implementer(IExecutable)
@adapter(Interface, IFieldCondition, Interface)
class FieldConditionExecutor:
    """The executor for this condition.

    This is registered as an adapter in configure.zcml
    """

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

    def __call__(self):
        object = self.event.object
        field = self.element.field
        value = self.element.value
        
        # Specify the field to check for  
        
        if field and value:
            attribute = getattr(object, field, None)
            field_type = type(attribute) if attribute is not None else None
            if field_type is not None:
                converted_value = field_type(value)
                if getattr(object, field, None) == converted_value:
                    return True
        
        return False
    


class FieldAddForm(AddForm):
    """An add form for tag expression condition."""

    schema = IFieldCondition
    label = _("Add Field Condition")
    description = _(
        "A Field condition makes the rule apply "
        "only if Field is not False in context."
    )
    form_name = _("Configure element")

    def create(self, data):
        c = FieldCondition()
        form.applyChanges(self, c, data)
        return c


class FieldAddFormView(ContentRuleFormWrapper):
    form = FieldAddForm


class FieldEditForm(EditForm):
    """An edit form for Field condition"""

    schema = IFieldCondition
    label = _("Edit Field Condition")
    description = _(
        "A Field condition makes the rule apply "
        "only if Field value is not False in context."
    )
    form_name = _("Configure element")


class FieldEditFormView(ContentRuleFormWrapper):
    form = FieldEditForm
1 Like