You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* pick migrate docs
* Enhance introduction with term definitions from https://zopeevent.readthedocs.io/en/latest/usage.html
* - Clean up grammar
- Enhance with MyST syntax `{file}` and `{lineno-start=1}`
* remove event handlers
---------
Co-authored-by: Steve Piercy <[email protected]>
A _subscriber_ is a callable object that takes one argument, an object that we call the _event_.
15
+
16
+
_Events_ are objects that represent something happening in a system.
17
+
They are used to extend processing by providing processing plug points.
18
+
19
+
A _notification_ alerts subscribers that an event has occurred.
20
+
21
+
The {term}`Zope Component Architecture`'s [`zope.event`](https://zopeevent.readthedocs.io/en/latest/) package is used to manage subscribable events in Plone.
22
+
23
+
The Plone event system has some notable characteristics:
24
+
25
+
- It's simple.
26
+
- The calling order of subscribers is random.
27
+
You can't set the order in which event handlers are called.
28
+
- Events can't be cancelled.
29
+
All handlers will always get the event.
30
+
- Event handlers can't have return values.
31
+
- Exceptions raised in an event handler will interrupt the request processing.
32
+
33
+
34
+
## Register an event handler
35
+
36
+
Plone events can be scoped:
37
+
38
+
- globally (no scope)
39
+
- per content type
40
+
- per behavior or marker interface
41
+
42
+
43
+
### Register an event handler on content type creation
44
+
45
+
The following example demonstrates how to register an event handler when a content type is created.
46
+
47
+
In your {file}`.product/your/product/configure.zcml` insert the following code.
48
+
49
+
{lineno-start=1}
50
+
```xml
51
+
<subscriber
52
+
for=".interfaces.IMyContentTypeClass
53
+
zope.lifecycleevent.IObjectCreatedEvent"
54
+
handler=".your_python_file.your_method"
55
+
/>
56
+
```
57
+
58
+
The second line defines to which interface you want to bind the execution of your code.
59
+
Here, the event handler code will only be executed if the object is a content type providing the interface `.interfaces.IMyContentTypeClass`.
60
+
If you want this to be interface agnostic, insert an asterix `*` as a wildcard instead.
61
+
62
+
The third line defines the event on which this should happen, which is `IObjectCreatedEvent`.
63
+
For more available possible events to use as a trigger, see {ref}`subscribers-event-handlers`.
64
+
65
+
The fourth line gives the path to the callable function to be executed.
66
+
67
+
Create your {file}`.product/your/product/your_python_file.py` and insert the following code.
68
+
69
+
```python
70
+
defyour_subscriber(object, event):
71
+
# do something with your created content type
72
+
```
73
+
74
+
75
+
### Subscribe to an event using ZCML
76
+
77
+
Subscribe to a global event using {term}`ZCML` by inserting the following code in your {file}`.product/your/product/configure.zcml`.
0 commit comments