简体   繁体   English

如何覆盖z3c.form按钮动作处理程序?

[英]How to override a z3c.form button action handler?

The default Dexterity add form registers the save button and handler thus: 默认的Dexterity添加表单注册了保存按钮和处理程序:

@button.buttonAndHandler(_('Save'), name='save')
def handleAdd(self, action):
    data, errors = self.extractData()
    if errors:
        self.status = self.formErrorsMessage
        return
    obj = self.createAndAdd(data)
    if obj is not None:
        # mark only as finished if we get the new object
        self._finishedAdd = True
        IStatusMessage(self.request).addStatusMessage(
            self.success_message, "info"
        )

How can I override (just) the handler with my own? 我怎样才能用我自己覆盖(只)处理程序? I'd prefer to just register some adapter, but if registering a subclassed custom form is the only option, then that's acceptable, too. 我宁愿只注册一些适配器,但如果注册子类自定义表单是唯一的选择,那么这也是可以接受的。

According to what you need, it could be enough to just override createAndAdd, but generally speaking you could do something similar: 根据你的需要,覆盖createAndAdd就足够了,但一般来说你可以做类似的事情:

In particular you can play with the handler of the original class doing something similar (line 50): 特别是你可以使用原始类的处理程序执行类似的操作(第50行):

@button.buttonAndHandler(_(u'I am sure, delete now'), name='Delete')
def handle_delete(self, action):
    base_handler = super(PIDeleteConfirmationForm, self).handle_delete
    return base_handler(self, action)

Of course you can add your custom code before and after the base_handler invocation. 当然,您可以在base_handler调用之前和之后添加自定义代码。

In addition you can also play with the updateActions method (see line 28). 此外,您还可以使用updateActions方法(参见第28行)。

Remember that when you want to ovverride buttons you have to override all of them. 请记住,当您想要ovverride按钮时,您必须覆盖所有按钮。

Another tip is that, in order to customize your ++add++your.portal.type traverser you have to register an homonymous named adapter: 另一个提示是,为了自定义你的++ add ++ your.portal.type遍历器,你必须注册一个同名的命名适配器:

<adapter
  for="Products.CMFCore.interfaces.IFolderish
       Products.CMFDefault.interfaces.ICMFDefaultSkin
       plone.dexterity.interfaces.IDexterityFTI"
  provides="zope.publisher.interfaces.browser.IBrowserPage"
  factory=".mytype.AddView"
  name="your.portal.type"
/>

See http://docs.plone.org/develop/plone/content/dexterity.html#custom-add-form-view http://docs.plone.org/develop/plone/content/dexterity.html#custom-add-form-view

Handlers don't have global registration (and form local registration is only button specific), so you cannot override just the handlers. 处理程序没有全局注册(表单本地注册只是按钮特定),因此您不能只覆盖处理程序。 Technically you could (at least for edit form) override the default button action handler, which does the lookup for all handlers, but the cleanest solution is just to subclass and override the form. 从技术上讲,你可以(至少对于编辑形式)覆盖默认的按钮动作处理程序,它对所有处理程序进行查找,但最干净的解决方案只是子类化并覆盖表单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM