我有:
我正在尝试自定义编辑表单:
问题:
那么,如何在灵巧内容类型的编辑表单中添加新字段而不更改其架构和保存时的自定义行为?
简单示例:拥有一个带有字段名称和年份的内容类型Car ...我希望编辑不仅可以编辑名称和年份,还可以编辑其生产者的电话号码(保存为生产者的值时保存)内容类型)。
我的WIP(已解决以覆盖编辑表单,但不知道如何继续):
<browser:page
for=".IMyContentType"
name="edit"
class=".views.EditForm"
permission="cmf.ModifyPortalContent"
/>
from plone.dexterity.browser import edit
class EditForm(edit.DefaultEditForm):
pass
答案 0 :(得分:2)
在这里你可以找到基本的DefaultEditForm代码:
https://github.com/plone/plone.dexterity/blob/master/plone/dexterity/browser/edit.py#L20
要执行其他操作,您需要覆盖handleApply方法(如果要在实际内容类型中保存字段值,则保留默认操作)并提供其他代码。
如果要添加其他字段,可以覆盖提供这些附加字段的fields属性,或使用updateWidgets和updateFields方法添加这些字段。这里有一些文档:
答案 1 :(得分:1)
最好使用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)