plonedexterityplone-4.x

Plone - How can I setDefaultPage in an event subscriber?


I have a folderish dexterity content type and I have an event subscriber. When the content type is created, I create a Collection, which shows the children in the container according to several parameters. After the collection is created, I try to set the default page of the container to the collection.

def myContainerAdded(my_container, event):
    #get container
    #set advanced query for collection
    #create collection with api.create
    my_container.setDefaultPage(new_collection.id)

The subscriber in configure.zcml

<subscriber
  for="my.product.my_container.IMyContainer
       zope.lifecycleevent.interfaces.IObjectAddedEvent" 
  handler=".events.myContainerAdded" />

Unfortunately, the default page is not being fully set. It just shows the container page, but the Collection is selected under the 'Display' drop down. If I click "Change content item as default view" and select the collection, it does change the default page to the collection.

Earlier, I was using a "setuphandler" to setup a folder structure (as opposed to an add event), and setDefaultPage was working. Am I forgetting a step since I'm attempting this through an event?

I am using plone.4.3.

Edit: I also tried:

my_container.default_page = new_collection.id

Edit: I found something interesting. I temporarily commented out the code related to the collection in the event subscriber. I manually added the collection to the container object and then set the default page of the container to the collection. The container's default page was the collection.

Maybe something isn't getting indexed right?


Solution

  • In fact everything went well, it's just that after creating a Dexterity-based content-type, one will land on the default-view's URL, meaning '/view' is appended to the item's URL, which is an alias to the default-view-method and here resolves to the 'folder_listing'-template.

    To overcome this quickly, you can add a redirect to the object's URL in the subscriber's method, without any view-name appended to the URL:

    event.REQUEST.RESPONSE.redirect(my_container.absolute_url())