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
**How to create a basic behavior that provides form fields**
12
+
This chapter describes how to create a basic behavior that provides form fields.
13
13
14
-
The following example is based on the [collective.gtags] product.
15
-
It comes with a behavior that adds a *tags* field to the “Categorization” fieldset, storing the actual tags in the Dublin Core *Subject* field.
14
+
The following example is based on the [`collective.gtags`](https://pypi.org/project/collective.gtags/) product.
15
+
It comes with a behavior that adds a `tags` field to the `Categorization` fieldset, storing the actual tags in the Dublin Core `Subject` field.
16
16
17
-
*collective.gtags* is a standard package, with a *configure.zcml*, a GenericSetup profile, and a number of modules.
18
-
We won’t describe those here, though, since we are only interested in the behavior.
17
+
`collective.gtags` is a standard package, with a `configure.zcml`, a GenericSetup profile, and a number of modules.
18
+
We won't describe those here, though, since we are only interested in the behavior.
19
19
20
-
First, there are a few dependencies in *setup.py*:
20
+
First, there are a few dependencies in {file}`setup.py`.
21
21
22
22
```python
23
23
install_requires=[
24
-
...,
25
-
'plone.behavior',
26
-
'zope.schema',
27
-
'zope.interface',
28
-
'zope.component',
24
+
#...,
25
+
"plone.behavior",
26
+
"zope.schema",
27
+
"zope.interface",
28
+
"zope.component",
29
29
],
30
30
```
31
31
32
-
Next, we have *behaviors.zcml*, which is included from *configure.zcml* and contains all necessary configuration to set up the behaviors.
33
-
It looks like this:
32
+
Next, we have {file}`behaviors.zcml`, which is included from {file}`configure.zcml`, and contains all the necessary configuration to set up the behaviors.
34
33
35
34
```xml
36
35
<configure
@@ -51,26 +50,26 @@ It looks like this:
51
50
</configure>
52
51
```
53
52
54
-
We first include the *plone.behavior meta.zcml* file, so that we get access to the *\<plone:behavior />* ZCML directive.
53
+
We first include the `plone.behavior meta.zcml` file, so that we get access to the `<plone:behavior />` ZCML directive.
55
54
56
-
The behavior itself is registered with the *\<plone:behavior />* directive.
57
-
We set a *title* and a *description*, and then specify the **behavior interface** with the *provides* attribute.
55
+
The behavior itself is registered with the `<plone:behavior />` directive.
56
+
We set a `title` and a `description`, and then specify the `behavior interface` with the `provides` attribute.
58
57
This attribute is required, and is used to construct the unique name for the behavior.
59
-
In this case, the behavior name is *collective.gtags.behaviors.ITags*, the full dotted name to the behavior interface.
60
-
When the behavior is enabled for a type, it will be possible to adapt instances of that type to *ITags*.
61
-
That adaptation will invoke the factory specified by the *factory* attribute.
58
+
In this case, the behavior name is `collective.gtags.behaviors.ITags`, the full dotted name to the behavior interface.
59
+
When the behavior is enabled for a type, it will be possible to adapt instances of that type to `ITags`.
60
+
That adaptation will invoke the factory specified by the `factory` attribute.
62
61
63
-
The *behaviors.py* module looks like this:
62
+
The following is the {file}`behaviors.py` module.
64
63
65
64
```python
66
-
"""Behaviours to assign tags (to ideas).
65
+
"""Behaviors to assign tags (to ideas).
67
66
68
-
Includes a form field and a behaviour adapter that stores the data in the
67
+
Includes a form field and a behavior adapter that stores the data in the
69
68
standard Subject field.
70
69
"""
71
70
72
71
from plone.dexterity.interfaces import DexterityContent
73
-
# if your package was made with mr.bob, add your MessageFactory like this:
72
+
# if your package was made with mr.bob, add your MessageFactory as shown:
74
73
from collective.mypackage import _
75
74
from plone.autoform.interfaces import IFormFieldProvider
76
75
from plone.supermodel import directives
@@ -86,9 +85,9 @@ class ITags(model.Schema):
86
85
"""
87
86
88
87
directives.fieldset(
89
-
'categorization',
90
-
label=_('Categorization'),
91
-
fields=('tags',),
88
+
"categorization",
89
+
label=_("Categorization"),
90
+
fields=("tags",),
92
91
)
93
92
94
93
tags = Tags(
@@ -120,24 +119,20 @@ class Tags(object):
120
119
self.context.setSubject(tuple(value))
121
120
```
122
121
123
-
We first define the *ITags* interface, which is also the behavior interface.
124
-
Here, we define a single attribute, *tags*, but we could also have added methods and additional fields if required.
122
+
We first define the `ITags` interface, which is also the behavior interface.
123
+
Here we define a single attribute, `tags`, but we could also have added methods and additional fields if required.
125
124
Naturally, these need to be implemented by the behavior adapter.
126
125
127
-
Since we want this behavior to provide form fields, we derive the behavior interface from *model.Schema* and set form hints using
128
-
*plone.supermodel.directives*.
129
-
We also mark the *ITags* interface with *IFormFieldProvider* to signal that it should be processed for form fields by the standard forms.
130
-
See the [Dexterity Developer Manual] for more information about setting form hints in schema interfaces.
126
+
Since we want this behavior to provide form fields, we derive the behavior interface from `model.Schema` and set form hints using `plone.supermodel.directives`.
127
+
We also mark the `ITags` interface with `IFormFieldProvider` to signal that it should be processed for form fields by the standard forms.
128
+
See the {doc}`Dexterity Developer Manual <../index>` for more information about setting form hints in schema interfaces.
131
129
132
-
If your behavior does not provide form fields, you can just derive from *zope.interface.Interface* and omit the *alsoProvides()* line.
130
+
If your behavior does not provide form fields, you can just derive from `zope.interface.Interface` and omit the `alsoProvides()` line.
133
131
134
132
Next, we write the class that implements the behavior adapter and acts as the adapter factory.
135
-
Notice how it implements the behavior interface (*ITags*), and adapts a broad interface *(IDexterityContent*).
133
+
Notice how it implements the behavior interface (`ITags`), and adapts a broad interface `(IDexterityContent`).
136
134
The behavior cannot be enabled on types not supporting this interface.
137
-
In many cases, you will omit the *adapter()* line, provided your behavior is generic enough to work on any context.
135
+
In many cases, you will omit the `adapter()` line, provided your behavior is generic enough to work on any context.
138
136
139
137
The adapter is otherwise identical to any other adapter.
140
-
It implements the interface, here by storing values in the *Subject* field.
0 commit comments