What I am trying to achieve is a dynamic Dexterity Add/Edit form based on context but I''m not sure that this is possible, yet. Thus:
Let's say that I have the following folder site structure:
Vehicles
Cars
Trucks
Pets
Cats
Fashion
Kids
Shoes
.......
Now, I will define a MagicItem Dexterity c-type with this IMagicBehavior enabled.
This IMagicBehavior should return a set of fields when I add the MagicItem within Cars, another one when I add it within Cats and yet another one when adding within Kids > Shoes
The info for this custom schema is somehow available on each section.
The site structure grows exponentially, thus adding custom content-types for each site section is not an option.
collective.taxonomy does some dynamic magic stuff in this direction, but it's quite hard to follow.
Thus, I would appreciate if someone has a better example, or can help somehow with this.
Following the documentation link you provided I managed to override the EditForm and AddForm like:
""" Custom Add/Edit forms
"""
from plone.dexterity.browser import edit
from plone.dexterity.browser import add
from plone.dexterity.utils import getAdditionalSchemata
class EditForm(edit.DefaultEditForm):
""" Custom edit form """
@property
def additionalSchemata(self):
for schema in super(EditForm, self).additionalSchemata:
yield schema
#
# Custom code here
#
class AddForm(add.DefaultAddForm):
portal_type = 'magic'
@property
def additionalSchemata(self):
for schema in super(AddForm, self).additionalSchemata:
yield schema
#
# Custom code here
#
class AddView(add.DefaultAddView):
form = AddForm
Still, found something interesting digging into the code: The getAdditionalSchemata method called by additionalSchemata has support also for context.
Are there any pitfalls using this approach?
Any chance that fields might disappear in copied / moved content ? If you move content from "Fashion" folder to "Sales" and edit it there ?
Traceback (innermost last):
* Module ZPublisher.Publish, line 138, in publish
* Module ZPublisher.mapply, line 77, in mapply
* Module ZPublisher.Publish, line 48, in call_object
* Module plone.z3cform.layout, line 63, in __call__
* Module plone.z3cform.layout, line 47, in update
* Module plone.dexterity.browser.add, line 134, in update
* Module plone.z3cform.fieldsets.extensible, line 65, in update
* Module plone.z3cform.patch, line 30, in GroupForm_update
* Module z3c.form.group, line 145, in update
* Module plone.app.z3cform.csrf, line 22, in execute
* Module z3c.form.action, line 98, in execute
* Module z3c.form.button, line 315, in __call__
* Module z3c.form.button, line 170, in __call__
* Module plone.dexterity.browser.add, line 109, in handleAdd
* Module z3c.form.form, line 263, in createAndAdd
* Module plone.dexterity.browser.add, line 76, in create
* Module plone.dexterity.browser.add, line 193, in _applyChanges
* Module z3c.form.datamanager, line 91, in set
* Module z3c.form.datamanager, line 66, in adapted_context
TypeError: ('Could not adapt', <Acquisition.ImplicitAcquisitionWrapper object at 0x7f36bdaa75f0>, <SchemaClass...
Please share if you get this to work, I have a feeling this will be useful one day.
PS: I had a (weird?) user case where I used the add-form to add the 'important fields' and the 'redirect' to the edit form on save (for all the 'not required/optional fields'). Could work as a workaround if everything else fails (?)
Defently not 'optimising' , but if it was possible to have a 'folder full edit view' where you edited all the items in a folder at the same time… that would be magic :)
Have you seen collective.ambidexterity? Being that it provides TTW (dynamic?) editing abilities for validators, defaults and vocabularies, maybe you could find some inspiration or use the package as a stepping stone for what you want to achieve.
I can see a dynamic schema being very useful in situations where you have to work with a vendor defined API. Just yesterday, I was looking at this mess https://developers.plentymarkets.com/rest-doc
Here you would have several dozen "types" of endpoints, which are individually described. The description could be processed by Plone, and on-the-fly create the required dexterity add and edit forms needed to create, read, update, delete content within the third party platform...