Add supplementary views to custom content types

by Sverre Andreas Dannbolt last modified Dec 30, 2008 03:03 PM
This howto explains how to add supplementary views to custom content types. These supplementary views are selected specifically for each folder TTW. One example is standard Plone folders, where you can choose between e.g. table view and standard view. Sometimes, you want to do this in your custom content type as well.

First, if you don't know what Archetypes are or want to learn how to make one, take a look at the Archetypes Developer Manual and then come back to this howto :-)

Second, you should make sure default_view and suppl_view are included in your content type class.

Examples from Plone

I'll start with an example class, this one is copied from the ATDocument of the product ATContentTypes.

class ATDocument(ATCTContent, HistoryAwareMixin):
    """A page in the site. Can contain rich text."""

    schema         =  ATDocumentSchema

    content_icon   = 'document_icon.gif'
    meta_type      = 'ATDocument'
    portal_type    = 'Document'
    archetype_name = 'Page'
    default_view   = 'document_view'
    immediate_view = 'document_view'
    suppl_views    = ()
    _atct_newTypeFor = {'portal_type' : 'CMF Document', 'meta_type' : 'Document'}
    typeDescription= 'A page in the site. Can contain rich text.'
    typeDescMsgId  = 'description_edit_document'
    assocMimetypes = ('application/xhtml+xml', 'message/rfc822', 'text/*',)
    assocFileExt   = ('txt', 'stx', 'rst', 'rest', 'py',)
    cmf_edit_kws   = ('text_format',)

    __implements__ = (ATCTContent.__implements__,
                      IATDocument,
                      HistoryAwareMixin.__implements__,
                     )

    [and so on]

As you can see, suppl_views for this content type is empty, while default_view is set to 'document_view'.

Another example from ATContentTypes is ATFolder, which has these properties:

default_view   = 'folder_listing'
suppl_views    = ('folder_summary_view', 'folder_tabular_view', 'atct_album_view')

Your own definition

The four options in the display menu of a folder is then listing, summary, tabular and album view. For our own product, we could add:

default_view = 'my_default_view'
suppl_views = ('my_not_so_default_view', 'my_very_little_default_view',)

Making the templates and adding metadata

Now we need to make the templates themselves, and to give them more human names than 'my_not_so_default_view' and 'my_default_view'.

First, add the files my_default_view.pt, my_not_so_default_view.pt, and my_very_little_default_view.pt to the skins folder of your custom content type. Then add the files my_default_view.pt.metadata, my_not_so_default_view.pt.metadata, and my_very_little_default_view.pt.metadata to the same folder.

The metadata files should contain this information:

[default]
title=The Title of My Default View

Mixin-classes - Mostly just for ArchGenXML-users

At this point, everything might work well. However, if you use ArchGenXML there could be some problems with suppl_views not being supported. ArchGenXML uses other classes as base for the custom content types than ATContentTypes, so you might have to add what is called a Mixin-class.

First, import the Mixin-class by adding this to the python file describing your content class:

from Products.CMFDynamicViewFTI.browserdefault import BrowserDefaultMixin

Then, tell your content class to inherit from BrowserDefaultMixin by replacing what might look like this:

class MyCustomFolderType(OrderedBaseFolder):

with this:

class MyCustomFolderType(BrowserDefaultMixin, OrderedBaseFolder):

You also need to «implement» this, by replacing what might look like this:

__implements__ = (getattr(OrderedBaseFolder,'__implements__',()),)

with this:

__implements__ = (getattr(BrowserDefaultMixin,'__implements__',()),) + (getattr(OrderedBaseFolder,'__implements__',()),)

Then it's just to restart Zope, reload your AT product and select whatever view you like the most :-).