Skip to content

Commit 27c63e9

Browse files
committed
Tidy creating-and-registering-behaviors.md
1 parent dd6771f commit 27c63e9

File tree

1 file changed

+38
-43
lines changed

1 file changed

+38
-43
lines changed

plone.app.dexterity/behaviors/creating-and-registering-behaviors.md

Lines changed: 38 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,35 @@
11
---
22
myst:
33
html_meta:
4-
"description": ""
5-
"property=og:description": ""
6-
"property=og:title": ""
7-
"keywords": ""
4+
"description": "How to create a basic behavior that provides form fields for content types in Plone"
5+
"property=og:description": "How to create a basic behavior that provides form fields for content types in Plone"
6+
"property=og:title": "How to create a basic behavior that provides form fields for content types in Plone"
7+
"keywords": "Plone, behaviors, content types, create, register"
88
---
99

1010
# Creating and registering behaviors
1111

12-
**How to create a basic behavior that provides form fields**
12+
This chapter describes how to create a basic behavior that provides form fields.
1313

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.
1616

17-
*collective.gtags* is a standard package, with a *configure.zcml*, a GenericSetup profile, and a number of modules.
18-
We wont 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.
1919

20-
First, there are a few dependencies in *setup.py*:
20+
First, there are a few dependencies in {file}`setup.py`.
2121

2222
```python
2323
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",
2929
],
3030
```
3131

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.
3433

3534
```xml
3635
<configure
@@ -51,26 +50,26 @@ It looks like this:
5150
</configure>
5251
```
5352

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.
5554

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.
5857
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.
6261

63-
The *behaviors.py* module looks like this:
62+
The following is the {file}`behaviors.py` module.
6463

6564
```python
66-
"""Behaviours to assign tags (to ideas).
65+
"""Behaviors to assign tags (to ideas).
6766
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
6968
standard Subject field.
7069
"""
7170

7271
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:
7473
from collective.mypackage import _
7574
from plone.autoform.interfaces import IFormFieldProvider
7675
from plone.supermodel import directives
@@ -86,9 +85,9 @@ class ITags(model.Schema):
8685
"""
8786

8887
directives.fieldset(
89-
'categorization',
90-
label=_('Categorization'),
91-
fields=('tags',),
88+
"categorization",
89+
label=_("Categorization"),
90+
fields=("tags",),
9291
)
9392

9493
tags = Tags(
@@ -120,24 +119,20 @@ class Tags(object):
120119
self.context.setSubject(tuple(value))
121120
```
122121

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.
125124
Naturally, these need to be implemented by the behavior adapter.
126125

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.
131129

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.
133131

134132
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`).
136134
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.
138136

139137
The adapter is otherwise identical to any other adapter.
140-
It implements the interface, here by storing values in the *Subject* field.
141-
142-
[collective.gtags]: http://svn.plone.org/svn/collective/collective.gtags
143-
[dexterity developer manual]: ../index.html
138+
It implements the interface, here by storing values in the `Subject` field.

0 commit comments

Comments
 (0)