RelatedItems widget bug when the object is inside a navigation root

Hi!

after defining a RelatedItems field in dexterity TTW, if the object instance is inside a navigation root the widget is unable to work correctly. This is because when it is a field widget, RelatedItemsFieldWidget uses @@getSource to query the catalog and this view overwrite the path using plone.app.querystring.queryparser._navigationPath in a wrong way (basically it mess with the path) when the object is under a folder that is a navigation root. You can see it easily going to https://classic.demo.plone.org/en/, add a relationchoice field to the Document CT using the CT control panel, then edit a page and try to use it. You can choose only current folder items.

This behaviour is very hard to fix (no test to see how it should work) so, after reading the code and trying to understand how things works, I've found this solution:

In the xml model (you can edit TTW in the CT control panel -> the type -> model editor):

<field name="myfield" type="z3c.relationfield.schema.RelationChoice">
<description></description>
<required>False</required>
<title>My Field</title>
<form:widget type="mymodule.custom.MyFieldWidgetFactory"/>
</field>

and in custom.py:

@adapter(schema.interfaces.IField, form.interfaces)
@implementer(form.interfaces.IFieldWidget)
def MyFieldWidgetFactory(field, request):
    """
    A special widget constructor setting up widget parameters for RelatedItemsField
    """
    widget = RelatedItemsFieldWidget(field, request)
    widget.vocabulary_override = True
                           
    return widget

Maybe there's an xml only solution, like a vocabulary_override tag inside form:widget and no type set but I've not time to find out. I've written more details on the problem in the #classic-ui Discord channel.

1 Like

I've added an issue here: RelatedItems field added TTW does not work correctly inside a navigation root · Issue #214 · plone/plone.app.z3cform · GitHub for reference.

Many, many years ago, I had the same problem, and I ended up writing my own vocabulary (without getSource).

I remember having been bitten by this in Plone 5.1 and 5.2 sites.

Don't think this is related to plone.app.z3cform but how the base path is configured in your widget. From looking at my crappy code, finding the wrong root folder is not limited to TTW XML fields.

Will take a walk down memory lane and see what I can find. I am pretty sure some of my posts from 2020/21 are about this or very similar (will add links if I can find).

Edit: we had this very same discussion in 2019 :wink: