Description
How to add event hooks to your Plone code to perform actions when something happens on a Plone site.
This document briefly discusses event handling using the zope.event module. The Zope Component Architecture's zope.event package is used to manage subscribeable events in Plone.
Some of the notable characteristics of the Plone event system are:
For more information, see:
Note
Starting Plone 4, using grok method is recommended as it is simpler
Example subscription which subscribes add and edit events for an content item type:
from five import grok
from Products.Archetypes.interfaces import IObjectEditedEvent, IObjectInitializedEvent
class ORAResearcher(folder.ATFolder, orabase.ORABase, ResearcherMixin):
"""A Researcher synchronized from ORA.
"""
implements(IORAResearcher, IResearcher)
meta_type = "ORAResearcher"
schema = ORAResearcherSchema
# Callbacks for both add and edit events
@grok.subscribe(ORAResearcher, IObjectEditedEvent)
def object_edited(context, event):
orabase.object_edited(context, event)
@grok.subscribe(ORAResearcher, IObjectInitializedEvent)
def object_added(context, event):
orabase.object_added(context, event)
Example subscription which subscribes events without context:
# Really old stuff
from ZPublisher.interfaces import IPubStart
# Modern stuff
from five import grok
@grok.subscribe(IPubStart)
def check_redirect(e):
"""
Check if we have a custom redirect script in Zope application server root.
For more information, see:
Custom event example:
<subscriber
for=".interfaces.IMyObject
.interfaces.IMyEvent"
handler=".content.MyObject.myEventHandler"
/>
Life cycle events example:
<subscriber
zcml:condition="installed zope.lifecycleevent"
for=".interfaces.ISitsPatient
zope.lifecycleevent.IObjectModifiedEvent"
handler=".content.SitsPatient.objectModified"
/>
The following subscription is valid through the process life cycle. In unit tests, it is important to clear test event handlers between the test steps.
Example:
import zope.component
def my_event_handler(context, event):
"""
@param context: Zope object for which the event was fired for. Usually this is a Plone content object.
@param event: Subclass of event.
"""
pass
gsm = zope.component.getGlobalSiteManager()
gsm.registerHandler(my_event_handler, (IMyObject,IMyEvent))
Use zope.event.notify() to fire event objects to their subscribers.
Example of how to fire an event in unit tests:
import zope.event
from plone.postpublicationhook.event import AfterPublicationEvent
event = AfterPublicationEvent(self.portal, self.portal.REQUEST)
zope.event.notify(event)
Warning
Archetypes and Zope 3 events might not be compatible with each other. Please see links below.
Other resources:
Two different content event types are available and might work differently depending on your scenario:
Note
Products.Archetypes.interfaces.IObjectEditedEvent is fired after reindexObject() is called. If you manipulate your content object in a handler for this event, you need to manually reindex new values, or the changes will not be reflected in the portal_catalog.
Delete events can be fired several times for the same object. Some delete event transactions are rolled back.
The DCWorkflow events are low-level events that can tell you a lot about the previous and current states.
The source code of this file is hosted on GitHub. Everyone can update and fix errors in this document with few clicks - no downloads needed.
For basic information about updating this manual and Sphinx format please see Writing and updating the manual guide.