简体   繁体   中英

How to override a z3c.form button action handler?

The default Dexterity add form registers the save button and handler thus:

@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:

In particular you can play with the handler of the original class doing something similar (line 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.

In addition you can also play with the updateActions method (see line 28).

Remember that when you want to ovverride buttons you have to override all of them.

Another tip is that, in order to customize your ++add++your.portal.type traverser you have to register an homonymous named adapter:

<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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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