Using portal_factory
How to avoid empty objects when using your own content types
By default, when you add one of your custom AT content types to a folder and then click Cancel, you end up with an empty item in the folder. This is obviously not the behavior you want.
You can prevent this by adding your content types to portal_factory in your product's installer. Just add these lines of code to the install() method in Extensions/Install.py in your product directory:
portal_factory = getToolByName(self,"portal_factory")
if portal_factory:
factory_types = portal_factory.getFactoryTypes()
factory_types.update(FACTORY_TYPES)
portal_factory.manage_setPortalFactoryTypes(listOfTypeIds = factory_types)
FACTORY_TYPES is just a dictionary of your content type class names, each with the value 1. You should probably define it in your configuration module (such as config.py), like this:
FACTORY_TYPES = {
'vsLibraryBook': 1,
'vsLibraryEdition': 1,
'vsLibrarySection': 1,
'vsLibraryContent': 1
}
If your product is already installed, you can fix this manually by following these steps:
- Go to the instance of your plone site in the ZMI, then click on portal_factory.
- Click on the "Factory Types" tab.
- Find your custom content types and make sure they are checked.
- Click Save.
Voila! Your custom types will now act like the standard AT content types.
