HomeZope Page 3 - Plone Content Types With Archetypes
Mutators and Accessors - 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.
Archetypes enables you to manipulate the values of fields. When you manipulate the value of a field when it is set, you are using a mutator. When you manipulate the value of a field when it is requested, you are using an accessor. Both mutators and accessors are simple to create and use in content types.
Let's take another look at our Quotation product above. Let's say we want to modify the field for the quotation. When the user sets the value, we'll put quotation marks around everything. To do this, we simply create a method called setQuotation. The method will be automatically called when the value of the field is set. The method goes into our Quotation class:
class Quotation(BaseContent): ... def setQuotation(self, value): value = '"' + value + '"' self.getField('quotation').set(self, value)
Restart Zope and install the product again. Add a quotation and look at the result—the value of the quotation field now has quotation marks around it. However, our system has a serious flaw. Click the “edit” tab and notice how the value of the text area has quotation marks around it. Save it and look again. There are now two sets of quotation marks, which is no good. While we could modify our mutator to work around this, the easiest way to fix this is to use an accessor instead.
Accessors, too, are methods, but they don't have any sort of value argument. Here's an accessor that does what we want:
Remember to delete the mutator we defined above in order for our content type to function properly. Restart Zope and reinstall our product. Create a quotation, or edit one, and save it. If quotation marks are left over from our mutator, make sure to delete them. You should now see quotation marks wrapped around your quotation. Notice that they do not appear when editing.