Skip to content

Commit 9024f08

Browse files
committed
Tidy custom-content-classes.md
1 parent f9e0ab9 commit 9024f08

File tree

1 file changed

+26
-52
lines changed

1 file changed

+26
-52
lines changed
Lines changed: 26 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,66 @@
11
---
22
myst:
33
html_meta:
4-
"description": ""
5-
"property=og:description": ""
6-
"property=og:title": ""
7-
"keywords": ""
4+
"description": "How to add a custom content class implementation in Plone"
5+
"property=og:description": "How to add a custom content class implementation in Plone"
6+
"property=og:title": "How to add a custom content class implementation in Plone"
7+
"keywords": "Plone, custom, content, class"
88
---
99

1010
# Custom content classes
1111

12-
**Adding a custom implementation**
12+
This chapter describes how to add a custom content class implementation.
1313

14-
When we learned about configuring the Dexterity FTI,
15-
we saw the `klass` attribute and how it could be used to refer to either
16-
the `Container` or `Item` content classes.
17-
These classes are defined in the [plone.dexterity.content] module,
18-
and represent container (folder) and item (non-folder) types, respectively.
14+
When we learned about configuring the Dexterity FTI, we saw the `klass` attribute and how it could be used to refer to either the `Container` or `Item` content classes.
15+
These classes are defined in the [`plone.dexterity.content`](https://github.com/plone/plone.dexterity/blob/master/plone/dexterity/content.py) module, and represent container (folder) and item (non-folder) types, respectively.
1916

2017
For most applications, these two classes will suffice.
21-
We will normally use behaviors, adapters, event handlers and schema
22-
interfaces to build additional functionality for our types.
23-
In some cases, however, it is useful or necessary to override the class,
24-
typically to override some method or property provided by the base class
25-
that cannot be implemented with an adapter override.
26-
A custom class may also be able to provide marginally better performance by
27-
side-stepping some of the schema-dependent dynamic behavior found in the
28-
base classes.
18+
We will normally use behaviors, adapters, event handlers, and schema interfaces to build additional functionality for our types.
19+
In some cases, however, it is useful or necessary to override the class, typically to override some method or property provided by the base class that cannot be implemented with an adapter override.
20+
A custom class may also be able to provide marginally better performance by side-stepping some of the schema-dependent dynamic behavior found in the base classes.
2921
In real life, you are very unlikely to notice, though.
3022

31-
Creating a custom class is simple: simply derive from one of the
32-
standard ones, e.g.:
23+
To create a custom class, derive from one of the standard ones, as shown below
3324

34-
```
25+
```python
3526
from plone.dexterity.content import Item
3627

3728
class MyItem(Item):
3829
"""A custom content class"""
39-
...
4030
```
4131

42-
For a container type, wed do:
32+
For a container type, we'd do the following
4333

44-
```
34+
```python
4535
from plone.dexterity.content import Container
4636

4737
class MyContainer(Container):
4838
"""A custom content class"""
49-
...
5039
```
5140

5241
You can now add any required attributes or methods to this class.
5342

54-
To make use of this class, set the `klass` attribute in the FTI to its
55-
dotted name, e.g.
43+
To make use of this class, set the `klass` attribute in the FTI to its dotted name, as shown in the following example.
5644

5745
```xml
5846
<property name="klass">my.package.myitem.MyItem</property>
5947
```
6048

61-
This will cause the standard Dexterity factory to instantiate this class
62-
when the user submits the add form.
49+
This will cause the standard Dexterity factory to instantiate this class when the user submits the add form.
50+
51+
```{note}
52+
As an alternative to setting `klass` in the FTI, you may provide your own `IFactory` utility for this type in lieu of Dexterity's default factory (see [plone.dexterity.factory](https://github.com/plone/plone.dexterity/blob/master/plone/dexterity/factory.py)).
53+
However, you need to be careful that this factory performs all necessary initialization, so it is normally better to use the standard factory.
54+
```
6355

64-
:::{note}
65-
As an alternative to setting `klass` in the FTI,
66-
you may provide your own `IFactory` utility for this type in lieu of
67-
Dexterity’s default factory (see [plone.dexterity.factory]).
68-
However, you need to be careful that this factory performs all necessary
69-
initialisation, so it is normally better to use the standard factory.
70-
:::
7156

7257
## Custom class caveats
7358

7459
There are a few important caveats when working with custom content classes:
7560

76-
- Make sure you use the correct base class: either
77-
`plone.dexterity.content.Item` or
61+
- Make sure you use the correct base class: either `plone.dexterity.content.Item` or
7862
`plone.dexterity.content.Container`.
79-
- If you mix in other base classes,
80-
it is safer to put the `Item` or `Container` class first.
81-
If another class comes first, it may override the `__name__`,
82-
`__providedBy__`, `__allow_access_to_unprotected_subobjects__` and/or
83-
`isPrincipiaFolderish` properties, and possibly the `__getattr__()`
84-
and `__getitem__()` methods,
85-
causing problems with the dynamic schemata and/or folder item security.
86-
In all cases, you may need to explicitly set these attributes to the ones
87-
from the correct base class.
88-
- If you define a custom constructor, make sure it can be called with
89-
no arguments, and with an optional `id` argument giving the name.
90-
91-
[plone.dexterity.content]: http://pypi.python.org/pypi/plone.dexterity.content
92-
[plone.dexterity.factory]: http://pypi.python.org/pypi/plone.dexterity.factory
63+
- If you mix in other base classes, it is safer to put the `Item` or `Container` class first.
64+
If another class comes first, it may override the `__name__`, `__providedBy__`, `__allow_access_to_unprotected_subobjects__`, or `isPrincipiaFolderish` properties, and possibly the `__getattr__()` and `__getitem__()` methods, causing problems with the dynamic schemata or folder item security.
65+
In all cases, you may need to explicitly set these attributes to the ones from the correct base class.
66+
- If you define a custom constructor, make sure it can be called with no arguments, and with an optional `id` argument giving the name.

0 commit comments

Comments
 (0)