Current redirection URL after saving a DX edit/add form?

We have a browser view with a a list of links to other items (their @@edit view in particular. After clicking on the edit link and after saving/cancelling the edit form the browser should redirect to the original view (or a different URL specified somehow) instead of the @@view of the related context object...any idea how to solve this?

-aj

I got inspiration from this: http://stackoverflow.com/questions/23231104/allow-anonymous-users-to-add-dexterity-objects

You have to add a custom add form.

First you add a named adapter for the content type in my case that is cru with zcml:

    <adapter
        for="Products.CMFCore.interfaces.IFolderish
             zope.publisher.interfaces.browser.IDefaultBrowserLayer
             plone.dexterity.interfaces.IDexterityFTI"
        provides="zope.publisher.interfaces.browser.IBrowserPage"
        factory=".add_cru.CruAddView"
        name="cru"
        />
    <class class=".add_cru.CruAddView">
        <require
            permission="nordforsk.content.addCru"
            interface="zope.publisher.interfaces.browser.IBrowserPage"
            />
    </class>

pointing to the class:

from Acquisition import aq_inner
from plone.dexterity.browser import add
from plone.dexterity.utils import addContentToContainer

class CruAddForm(add.DefaultAddForm):
    portal_type = 'cru'

    def add(self, object):
        container = aq_inner(self.context)
        addContentToContainer(container, object, checkConstraints=False) #you only need checkConstraints=False for anonymous add
        self.immediate_view = container.absolute_url() #redirect here


class CruAddView(add.DefaultAddView):
    form = CruAddForm

For the edit form you probably have to look at https://github.com/plone/plone.dexterity/blob/master/plone/dexterity/browser/edit.py, the zcml would probably be different in that case.

This looks so scary but I will give it a try :smile:
-aj

In case like this I override the nextURL method in the add and edit view:

Follows an example taken from a wizard edit form:

seems about right, but you also need to override the add method of add add.py if you want it to redirect to a different view on add as well as it doesn't seem to use nextURL to handle redirect.

Not as scary and not as tricky as I thought.

The trick was to add a return_url=.... parameter to the related edit links and pick up the return_url parameter inside the nextURL() method from request/HTTP_REFERER.

-aj

1 Like

But the return URL will be lost if the form is submitted with validation errors, unless you hang on to it in a hidden input.

David Glick
(mobile)