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
Copy file name to clipboardExpand all lines: docs/backend/behaviors.md
+47-43Lines changed: 47 additions & 43 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,8 +13,8 @@ myst:
13
13
14
14
## What are Behaviors in Plone
15
15
16
-
In Plone, behaviors are a way to add additional, reusable functionality to content objects without modifying the objects themselves.
17
-
Behaviors are essentially small chunks of code that can be plugged onto content objects to provide new features or capabilities.
16
+
In Plone, behaviors are a way to add reusable functionality to content objects without modifying the objects themselves.
17
+
Behaviors are essentially small chunks of code that can be plugged onto content types to provide new features or capabilities.
18
18
19
19
A Plone behavior could be used to
20
20
@@ -29,8 +29,8 @@ Behaviors can be added to content types on an as-needed basis, allowing for a hi
29
29
Plone already provides lots of behaviors for the built-in content types.
30
30
31
31
Other behaviors are implemented as add-on products, which can be installed and configured through the Plone control panel.
32
-
Once a behavior has been installed, it can be applied to any content type by selecting it in the Dexterity configuration section of the Plone control panel.
33
-
This allows the object to gain the additional functionality provided by the behavior.
32
+
Once a behavior has been installed, it can be applied to any content type by selecting it in the {guilabel}`Content Types` control panel.
33
+
This allows items of this content type to gain the additional functionality provided by the behavior.
34
34
35
35
Overall, behaviors are an important part of the Plone content management system and allow for powerful customization and extensibility of content objects.
36
36
@@ -67,37 +67,39 @@ Overall, behaviors are an important part of the Plone content management system
67
67
| plone.tableofcontents | Table of contents | Adds a table of contents |
68
68
| plone.thumb_icon | Thumbs and icon handling | Options to suppress thumbs and/or icons and to override thumb size in listings, tables etc.
69
69
| plone.versioning | Versioning | Versioning support with CMFEditions |
70
+
| volto.blocks | Blocks | Enables Volto Blocks support |
## Adding or removing a behavior from a content type
72
74
73
-
There are two ways to add or remove a behavior on a type:
75
+
There are two ways to add or remove a behavior on a content type:
74
76
75
-
-through the web using the control panel,
76
-
-using a custom add-on GenericSetup profile.
77
+
-Through the web using the {guilabel}`Content Types`control panel.
78
+
-Using a custom add-on GenericSetup profile.
77
79
78
80
### Through the Web
79
81
80
-
1. Go to the {guilabel}`Site Setup` and chose the {guilable}`Content Types` control panel
81
-
2. Select the type where you want to add or remove a behavior
82
-
3. Then click on the {guilabel}`Behaviors` tab of the settings of the type.
82
+
1. Go to the {guilabel}`Site Setup` and chose the {guilabel}`Content Types` control panel.
83
+
2. Select the content type to which you want to add or remove a behavior.
84
+
3. Then click on the {guilabel}`Behaviors` tab of the settings of the content type.
83
85
4. A list of all available behaviors appears.
84
-
Select or unselect the checkbox of the behavior you want to add to or remove from the type.
86
+
Select or deselect the checkbox of the behavior you want to add to or remove from the type.
85
87
5. Save the form by clicking on the {guilabel}`Save` button at the bottom of the page.
86
88
87
89
### Using a GenericSetup profile
88
90
89
-
Given you already have a custom add-on with a `profiles/default` directory.
91
+
Given you already have a custom add-on with a `profiles/default` directory, and you created a custom behavior named `mybehavior.subtitle`.
90
92
Given you created a custom behavior named `mybehavior.subtitle`.
91
93
92
94
If you want to enable a behavior on an existing content type, create a new directory `types` under `profiles/default`.
93
-
In there create a file named the same as the content type you want to change.
94
-
In the example here, you want to add a behavior to the built-in Event type.
95
-
The file to create is named `Event.xml` and it is a Factory Type Information definition.
96
-
You need to change the behaviors configuration only so that all other parts can be omitted.
95
+
In the `types` directory, create a file named the same as the content type you want to change.
96
+
In the example here, you want to add a behavior to the built-in Event content type.
97
+
The file to create is named `Event.xml` and it is a {term}`Factory Type Information` (FTI) definition.
98
+
You need to change only the behaviors configuration.
99
+
All other parts can be ignored.
97
100
This looks like so:
98
101
99
-
```XML
100
-
102
+
```xml
101
103
<?xml version="1.0"?>
102
104
<object
103
105
i18n:domain="plone"
@@ -112,16 +114,16 @@ This looks like so:
112
114
113
115
After you apply the profile (or uninstall and install the custom add-on), the behavior is available on the Event content type.
114
116
115
-
## How behaviors are working
117
+
## How behaviors work
116
118
117
-
At the most basic level, a behavior is like a conditional adapter.
118
-
For a Dexterity content type, the default condition is, "is this behavior listed in the behaviors property in the FTI?"
119
+
At the most basic level, a behavior is like a conditional {term}`adapter`.
120
+
For a content type, the default condition is, "is this behavior listed in the behaviors property in the FTI?"
119
121
But the condition itself is an adapter; in rare cases, this can be overruled.
120
122
When a behavior is enabled for a particular object, it will be possible to adapt that object to the behavior's interface.
121
123
If the behavior is disabled, adaptation will fail.
122
124
123
125
A behavior consists at the very least of an interface and some metadata, namely a name, title, and description.
124
-
This is also called a schema-only interface
126
+
This is also called a schema-only interface.
125
127
126
128
In other cases, there is also a factory, akin to an adapter factory, which will be invoked to get an appropriate adapter when requested.
127
129
This is usually just a class that looks like any other adapter factory, although it will tend to apply to Interface, IContentish, or a similarly broad context.
@@ -139,32 +141,34 @@ This results in, among other things, a named utility providing `plone.behavior.i
139
141
This utility contains various information about the behavior, such as its name, title, interface, and (optional) factory and marker interface.
140
142
141
143
The utility name is the name attribute of the behavior directive.
142
-
Historically the full dotted name to the behavior interface is registered as name too, but usage is discouraged.
144
+
Historically the full dotted name to the behavior interface is registered as its name, too, but this usage is discouraged.
143
145
144
146
145
147
Behaviors are named adapters.
146
148
They adapt an interface, for which they are registered and they provide another interface with new functionality such as attributes and methods.
147
149
148
-
Dexterity types are transparently looking up behaviors registered for a type.
149
-
If a behavior provides new attributes it is provided as an attribute of the adapted object.
150
+
Content types transparently look up the behaviors registered for a content type.
151
+
If a behavior provides new attributes, it is provided as an attribute of the adapted object.
150
152
151
153
```{seealso}
152
154
The [README file of `plone.behavior`](https://github.com/plone/plone.behavior/blob/master/README.rst) explains the concepts and different ways to register a behavior in detail.
153
155
```
154
156
155
157
## Custom behaviors
156
158
157
-
From the last section you can take away, that there are two main types of behaviors:
159
+
There are two types of behaviors:
158
160
159
-
- Schema-only behaviors: These behaviors have only a schema with fields.
161
+
Schema-only behaviors
162
+
: These behaviors have only a schema with fields.
160
163
161
-
- Full behaviors: A python class containing the logic of the behavior, an interface or schema defining the contract of the behavior, and a marker interface applied to the content type.
164
+
Full behaviors
165
+
: A python class containing the logic of the behavior, an interface or schema defining the contract of the behavior, and a marker interface applied to the content type.
162
166
163
167
### Creating a schema-only behavior
164
168
165
169
Given you want to add a field `subtitle` to some existing content types of your custom add-on.
166
170
167
-
You need to create a file `subtitle.py` in your addon like so:
171
+
You need to create a file `subtitle.py` in your add-on:
168
172
169
173
```python
170
174
from plone.autoform.interfaces import IFormFieldProvider
@@ -185,33 +189,33 @@ class ISubtitleBehavior(model.Schema):
185
189
)
186
190
```
187
191
188
-
You need to add a ZCML snippet to the `configure.zcml` next to `subtitle.py` like so:
192
+
You need to add a ZCML snippet to the `configure.zcml` next to `subtitle.py`:
189
193
190
-
```XML
194
+
```xml
191
195
<plone:behavior
192
196
name="myproject.subtitle"
193
197
provides=".subtitle.ISubtitleBehavior"
194
198
title="Subtitle"
195
199
/>
196
200
```
197
201
198
-
After a restart of Plone, the behavior can be added to the type in the Content Types control panel.
199
-
The add and edit forms are containing a new field `Subtitle`.
202
+
After a restart of Plone, the behavior can be added to the content type in the {guilabel}`Content Types` control panel.
203
+
The add and edit forms contain a new field `Subtitle`.
200
204
201
205
This field is not displayed in most views.
202
-
To display the data entered in this field you need to modify the page template and access the field as `context.subtitle` there.
206
+
To display the data entered in this field, you need to modify the page template by adding the field `context.subtitle`.
203
207
204
208
### Creating a behavior with an adapter and factory
205
209
206
210
Given you want to display a price with different content types.
207
211
The price is stored as the net value on the type as a floating point number.
208
-
For display, you need at several places the VAT and the gross value.
212
+
For display, you need at several places the value added tax (VAT) and the gross value.
209
213
210
214
You create a schema with the net value for the form and attributes for the calculated values.
211
215
You create an adapter to calculate the VAT and gross values.
212
216
You need a marker interface to distinguish between context and adapter.
213
217
214
-
The Python code in a file `price.py` looks like so:
218
+
Add Python code in the file `price.py`:
215
219
216
220
```python
217
221
from plone.autoform.interfaces import IFormFieldProvider
@@ -277,12 +281,12 @@ The registration in the `configure.zcml` looks like so:
277
281
/>
278
282
```
279
283
280
-
After a restart of Plone, the behavior can be added to the type in the Content Types control panel.
281
-
The add and edit forms are containing a new field `Price (net)`.
284
+
After a restart of Plone, the behavior can be added to the content type in the {guilabel}`Content Types` control panel.
285
+
The add and edit forms contain a new field `Price (net)`.
282
286
283
287
This field is not displayed in most views.
284
-
To display the data entered in this field you need to modify the page template and access the `price_net` field as `context.price_net` there.
285
-
To access the `price_vat` and `price_gross` fields you need to get the adapter in your view class like so:
288
+
To display the data entered in this field, you need to modify the page template by adding the `price_net` field as `context.price_net`.
289
+
To access the `price_vat` and `price_gross` fields, you need to get the adapter in your view class:
286
290
287
291
```Python
288
292
from .price import IPriceBehavior
@@ -302,11 +306,11 @@ class SomeViewClass:
302
306
303
307
To add a behavior to your add-on, you can use PloneCLI as follows:
304
308
305
-
```bash
309
+
```shell
306
310
plonecli add behavior
307
311
```
308
312
309
-
This will create the behavior Python file in the `behaviors` folder where you can define your behaviors schema fields and registers the behavior in the `configure.zcml`.
313
+
This will create the behavior Python file in the `behaviors` folder, where you can define your behaviors schema fields, and registers the behavior in the `configure.zcml`.
0 commit comments