Patrick Downey
Patrick Downey

Reputation: 965

Plone setuphandler- In a setuphandler, how can I programatically add/create a folderish content type at root of Plonesite?

I have have made a folderish content type called supplier_folder, which displays a list of suppliers that can be added under it, and their information. I can add it through the navigation bar, but I would like to add it programatically during setup.

I have been following the tutorial on custom installer code (http://docs.plone.org/develop/addons/components/genericsetup.html#custom-installer-code-setuphandlers-py) and have looked at creating objects programatically (http://docs.plone.org/develop/plone/content/creating.html).

Unfortunately, the second article says I need to have a folder created. How can I get around this and add the supplier_folder object at the Plone Site outside of a folder?

Upvotes: 1

Views: 213

Answers (3)

jkubaile
jkubaile

Reputation: 91

There are a couple of ways to achieve this. The buildin mechanism is to use GenericSetup in combination with "structure" folder as described here: http://koansys.com/tech/create-plone-initial-content-with-generic-setup

In short you need the following:

  1. Create a folder "structure" in your Generic Setup profile (in general, under profiles/default)
  2. Create a .objects file with the following content: "suppliers,supplier_folder"
  3. in "structure" create a folder "suppliers" with a .properties file and content:

    [DEFAULT]
    title = Suppliers
    description = Some usefull description text
    

As far as I remember this is ok for simple structures likes your. If you have complex structures with folders and sub-folders and want more specific control you probably need to write python code. I made some stuff here: https://github.com/collective/zettwerk.setup/blob/master/zettwerk/setup/structure.py

But zettwerk.setup is not yet released, but you should be able to integrate the structure.py right into your project. Than you can the handle_structure method into your setuphandlers.py and passing a structure dict like this:

handle_structure(portal, [{'id': 'suppliers', 'portal_type': 'supplier_folder'}])

The advantage of this method is, that you can also control metadata like workflow state, default page setting, portlets, local roles and some others.

Upvotes: 1

hvelarde
hvelarde

Reputation: 2876

IIRC, only users with role Manager or Site Administrator can add content to the root of the site; you can overcome this limitation in two ways:

  1. by using the _constructInstance method as it bypasses the permissions when creating an item
  2. by switching roles inside your code with plone.api.env.adopt_roles

I personally prefer the second one.

you can see an example of a pretty complex setuphandlers.py in interlegis.portalmodelo.policy package.

Upvotes: 1

keul
keul

Reputation: 7819

When you create a new Plone site, it's also creating some default content types.

Look at how Plone do: https://github.com/plone/Products.CMFPlone/blob/1471828ee97a8dd84396bad4a3286be514819869/Products/CMFPlone/setuphandlers.py#L119

Upvotes: 1

Related Questions