Plone:灵活内容类型的自定义编辑表单,保存时具有自定义行为

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

我有:

我正在尝试自定义编辑表单:

问题:

那么,如何在不更改其架构并在保存时使用自定义行为的情况下,在敏捷内容类型的编辑表单中添加新字段?

简单示例:内容类型 Car 包含字段名称和年份...我想在编辑时不仅要编辑名称和年份,还要编辑 Phone 生产商编号(要保存另存为 Producer 内容类型的值)。

我的 WIP(解决了覆盖编辑表单的问题,但不知道如何继续):

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

from plone.dexterity.browser import edit

class EditForm(edit.DefaultEditForm):
    pass

在这里您可以找到基本的 DefaultEditForm 代码:​​

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

要执行其他操作,您需要覆盖 handleApply 方法(如果您想在实际内容类型中保存字段值,请保留默认操作)并提供您的其他代码。

如果您想添加额外的字段,您可以覆盖提供这些额外字段的字段属性,或者使用 updateWidgets 和 updateFields 方法来添加这些字段。这里有一些文档:

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

最好使用 handleApply,但这也有效:

  <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)