Personal tools
You are here: Home Documentation How To's How to enable versioning (History tab) for a custom content type

How to enable versioning (History tab) for a custom content type

by T. Kim Nguyen last modified Apr 29, 2009 12:07 PM

Enabling versioning on your custom content-types

The following is a hatchet copy-and-paste job from the above site.  It describes how you can code this functionality into your custom content type.

Instead, you can also manually add versioning to your content type by going to Site Setup, Types, select the content type, check the box for Allow Versioning, press Save.  Then in the ZMI, portal_diff, select the same content type, enter "any" in the text field, and choose "Compound Diff etc.", then press the Add button.

Hatchet Copy and Paste

Plone 3 includes a robust versioning system as well as a tool for viewing diffs, which allows you to easily see the changes between two revisions. This document explains how to integrate versioning and diff functionality with your custom Archetypes-based content-types.


Prerequisites

You'll need a Plone 3 instance and a custom product which contains at least one Archetypes-based content-type on which you want to enable versioning.

You'll also need to have the Working Copy Support (Iterate) product installed.  This product is part of the Plone core so to install it, all you need to do it visit the Add-on Products section (a.k.a. Quickinstaller) of the Plone control panel and select it for installation.

Creating a setup handler script for GenericSetup

The integration code we'll be writing here is best run as a setup handler using GenericSetup. If your product doesn't already have a GenericSetup profile and a custom setup handler, this tutorial provides instructions on how to create those.

Declaring versionable types in your setup handler

The portal_repository tool stores a list of content-types on which version is enabled. With the following code we create a list of the custom types on we which we want to activate versioning and then notify the repository tool to start versioning the types in this list.

If you copy the code below, make sure to edit the TYPES_TO_VERSION setting so that it contains a list of the types on which you want to activate versioning.

from Products.CMFCore.utils import getToolByName

from Products.CMFEditions.setuphandlers import DEFAULT_POLICIES



# put your custom types in this list

TYPES_TO_VERSION = ('Scientist', 'Article', 'Presentation')



def setVersionedTypes(portal):

  portal_repository = getToolByName(portal, 'portal_repository')

  versionable_types = list(portal_repository.getVersionableContentTypes())

  for type_id in TYPES_TO_VERSION:

  if type_id not in versionable_types:

  # use append() to make sure we don't overwrite any

  # content-types which may already be under version control

  versionable_types.append(type_id)

  # Add default versioning policies to the versioned type

  for policy_id in DEFAULT_POLICIES:

  portal_repository.addPolicyForContentType(type_id, policy_id)

  portal_repository.setVersionableContentTypes(versionable_types)

Now we call this function from the importVarious() function in our setup handler script.  Make sure to pass the portal object as a parameter.

def importVarious(context):

  """Miscellanous steps import handle

  """



  portal = context.getSite()



  setVersionedTypes(portal)


Enabling visual diffs on your versioned types

Now that you've enabled versioning, you'll probably want to enable visual diffs so you can compare the changes made between different versions of an object.

Starting in Plone 3.2 the diff tool can be configured via a GenericSetup configuration file. You'll want to create or edit the diff_tool.xml file in the /profiles directory of your product. ere's an example confirmation file that enables compound diffs on the 3 content-types used in the example above.

<?xml version="1.0"?>

<object>

  <difftypes>



  <type portal_type="Scientist">

  <field name="any" difftype="Compound Diff for AT types"/>

  </type>



  <type portal_type="Article">

  <field name="any" difftype="Compound Diff for AT types"/>

  </type>



  <type portal_type="Presentation">

  <field name="any" difftype="Compound Diff for AT types"/>

  </type>



  </difftypes>

</object>


For Plone versions earlier than 3.2, there is not a GenericSetup handler for configuring the diff tool, but you can create these settings through the ZMI using the portal_diff tool. In the Portal Type drop-down menu select the content-type on which you want to enable diffs.  In the Field name box type "any". For the Diff Type select "Compound Diff for AT Types". Finally click the Add Field button. Repeat these steps for each content-type.


Deploying your new versioning and diffs policy

To deploy these changes you'll need to re-run you product's GenericSetup policy. If your instance is not running in debug mode, you'll first need to restart your Zope instance to make your new filesystem code available.

Assuming you've used paster to create your product package, reinstalling your product in the Add-on Products section of the Plone control panel should be sufficient to re-run your GenericSetup policy. If your product's install method does not run your GenericSetup policy, you may need to visit the portal_setup tool in the ZMI and run it manually.


Verifying that versioning and visual diffs are now enabled

Visit an instance of one of the types on which you've enabled versioning.  Make some edits to one or more of the fields save those changes.  Next, visit the history tab for the item you just edited.  You should see a list showing two versions.  Click the link to compare versions you should see a diff showing you what has been changed between the two revisions.


Document Actions
  • Print this
  • Bookmarks
Help

For Plone technical support during normal work hours:

To request a new web site: