Description
How to add event hooks to your Plone code to perform actions when something happens on a Plone site.
This document briefly discuss about doing event handling using zope.event module. Zope component architecture’s ‘zope.event package <http://pypi.python.org/pypi/zope.event>’ is used to manage subscribeable events in Plone.
There are few notable characters in Plone event system
For more information
Example:
from five import grok
from Products.Archetypes.interfaces import IObjectEditedEvent
class ORAResearcher(folder.ATFolder):
meta_type = "ORAResearcher"
schema = ORAResearcherSchema
def doSomethingDependingOnTheData():
"""
Do something with the object
"""
@grok.subscribe(ORAResearcher, IObjectEditedEvent)
def object_edited(context, event):
"""
Catch edit events.
@param context: Object for which the event was fired
"""
# Call content type methods
context.doSomethingDependingOnTheData()
ノート
You need to have five.grok initialization code in your product’s root configure.zcml to be able to use Grok like event subscribers. Otherwise your event handlers won’t get called.
More information
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 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 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)
警告
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
ノート
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 are not reflected back to portal_catalog.
Delete events can be fired several times for the same object. Some delete event transactions are rolled back.