GhitaB
GhitaB

Reputation: 3437

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

I have:

I'm trying to customize the edit form:

The problem:

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

Upvotes: 1

Views: 762

Answers (2)

GhitaB
GhitaB

Reputation: 3437

Better use handleApply, but this is working, too:

  <browser:page
    for="my.package.IMyContentType"
    name="edit"
    class=".EditForm"
    permission="zope2.View"
    />

  <adapter
    provides="plone.z3cform.fieldsets.interfaces.IFormExtender"
    for="my.package.IMyContentType
    zope.publisher.interfaces.browser.IDefaultBrowserLayer
    .EditForm"
    factory=".EditFormExtender"
    />

from my.package.config import MessageFactory as _
from plone.dexterity.browser import edit
from plone.dexterity.interfaces import IDexterityEditForm
from plone.z3cform import layout
from zope.interface import classImplements
from plone.z3cform.fieldsets.extensible import FormExtender
from z3c.form.field import Fields
from z3c.form import util
from zope import schema


class EditFormExtender(FormExtender):
    def update(self):
        if self.request.REQUEST_METHOD == 'GET':
            # add fields
            new_field1 = schema.TextLine(
                __name__="New field 1",
                title=_(u'label_new_field1', default=u'New field 1'),
                description=_(u'help_new_field1',
                              default=u'Enter new field 1 value.'),
                required=False,
            )

            new_field2 = schema.TextLine(
                __name__="New field 2",
                title=_(u'label_new_field2', default=u'New field 2'),
                description=_(u'help_new_field2',
                              default=u'Enter new field 2 value.'),
                required=False,
            )

            self.form.fields += Fields(new_field1, new_field2)

        if self.request.REQUEST_METHOD == 'POST':
            # save values
            if 'form.buttons.save' in self.request.form:
                # do your custom save here

                prefix = 'form.widgets.'
                field_names = ['new_field1', 'new_field2']
                print "UPDATED:"
                for field_name in field_names:
                    print self.request.form.get(prefix + field_name)


class EditForm(edit.DefaultEditForm):
    """ Override MyContentType edit form
    """


EditView = layout.wrap_form(EditForm)
classImplements(EditView, IDexterityEditForm)

Upvotes: 1

Mikel Larreategi
Mikel Larreategi

Reputation: 156

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

Upvotes: 2

Related Questions