Search code examples
ploneplone-4.x

Plone: custom edit form for dexterity content type with custom behavior on save


I have:

  • a dexterity content type defined by schema (interface)
  • its default edit form

I'm trying to customize the edit form:

  • keeping current fields and validation as they are
  • adding new fields

The problem:

  • my values for the new fields I want not to be saved in my content type, but in other place.

So, how can I add new fields in an edit form of a dexterity content type without changing its schema and with custom behavior on save?

Simple example: Having a content type Car with fields name and year... I want on edit to edit not only the name and year, but also the Phone number of its producer (to be saved on save as value of Producer content type).

My WIP (solved to override the edit form, but no idea how to continue):

<browser:page
    for=".IMyContentType"
    name="edit"
    class=".views.EditForm"
    permission="cmf.ModifyPortalContent"
    />

from plone.dexterity.browser import edit

class EditForm(edit.DefaultEditForm):
    pass

Solution

  • Here you can find the base DefaultEditForm code:

    https://github.com/plone/plone.dexterity/blob/master/plone/dexterity/browser/edit.py#L20

    To do additional actions you need to override the handleApply method (preserving the default action if you want to save field values in the actual content-type) and there provide your additional code.

    If you want to add additional fields, you can override the fields attribute providing those additional fields or use the updateWidgets and updateFields methods to add those fields. Here you have some documentation:

    https://docs.plone.org/external/plone.app.dexterity/docs/advanced/custom-add-and-edit-forms.html#edit-forms