HomeZope Page 2 - Plone Content Types With Archetypes
A Quotation Content Type - Zope
Plone is an excellent content management system. You will probably find everything you need in the system itself, or from third party content types. If you don't, however, it is very easy to create your own content types, as this article will explain.
We'll start with the example I described in the previous section, a simple quotation content type. When an object based off the content type is added, text fields for the speaker of the quotation and the quotation will be presented. After they are filled out and the form is submitted, the object will be created. When the object is viewed, it will display the speaker and the quotation.
Create a folder called Quotation in the Products directory. This is where we'll store our content type's files. The first file we will create is config.py. This defines some constants that we'll use throughout our content type:
from Products.CMFCore.CMFCorePermissions import AddPortalContent
In config.py, we first define the permission required to manipulate our content type. Since our content type really isn't anything special, we just assign it to the generic AddPortalContent permission. We then define a constant called GLOBALS, which is used during the installation of our content type. Finally, we give our content type (or project/product, rather, since it's possible to have multiple content types in the same package) a name.
Next, we'll have to create our content type's schema, which will define the fields associated with the content type. When we edit quotations, we'll want a text field for the speaker and a larger text area for the quotation itself. All of this is specified in the schema:
from Products.Archetypes.public import * from Products.Quotation.config import *
class Quotation(BaseContent): "A simple quotation content type." schema = schema
The first thing we do is create our content type's schema. We create two text fields, one for the speaker and one for the quotation. Both are required to be filled out. The second one is also set to a text area. Next, we create the content type's class, where we simply copy the schema, and, finally, we register the content type.
Next is __init__.py, where we glue what we've done so far together:
from config import * from Products.Archetypes import process_types, listTypes from Products.CMFCore import utils
Besides importing our configuration file and various methods that we'll be using, the first thing we do is define a function and import our Quotation.py file. From there, we get some information about our package, the content types associated with it, constructors and factory type information, in that order. We then create a utils.ContentInit with this information, along with the package name and the permission we defined in config.py.
Finally, we have to create Install.py, which is responsible for installing our product. Create a folder called Extensions inside Quotation to place the install script in:
from Products.Archetypes.Extensions.utils import installTypes from Products.Archetypes.public import listTypes from Products.Quotation.config import PROJECTNAME, GLOBALS import StringIO
This simply installs our content type and inserts a short message into the installation log.
Our Quotation content type is now complete. Restart Zope and then install the product in the “Add/Remove Products” section of Plone. You should now be able to add an object based off our content type to a folder.
You can also create the files version.txt and README.txt. The contents of the former file will be added to the product's name, and the contents of the latter file will be displayed as the product's description.