Skip to content

Commit 357f0cd

Browse files
committed
add more info about RichText field and the usage of transforms and RichTextValue
1 parent 7075f95 commit 357f0cd

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed

docs/backend/fields.md

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ See [`z3c.relationfield`](https://pypi.org/project/z3c.relationfield/) for more
115115
| `RelationChoice` | `RelationValue` | A `Choice` field intended to store `RelationValue`s | See {ref}`Choice <fields-in-zope-schema-label>` |
116116

117117

118+
(backend-fields-richtext-label)=
119+
118120
### Fields in `plone.app.textfield`
119121

120122
See [`plone.app.textfield`](https://pypi.org/project/plone.app.textfield/) for more details.
@@ -124,6 +126,115 @@ See [`plone.app.textfield`](https://pypi.org/project/plone.app.textfield/) for m
124126
| `RichText` | `RichTextValue` | Stores a `RichTextValue`, which encapsulates a raw text value, the source MIME type, and a cached copy of the raw text transformed to the default output MIME type. | `IField`, `IRichText` |
125127

126128

129+
The RichText field allows for alternative markups and content filtering.
130+
131+
```python
132+
from plone.app.textfield import RichText
133+
from plone.supermodel import model
134+
135+
class ITestSchema(model.Schema):
136+
137+
body = RichText(title="Body text")
138+
```
139+
140+
The `RichText` field constructor can take the following arguments, in addition to the usual arguments for a `Text` field.
141+
142+
`default_mime_type`
143+
: A string representing the default MIME type of the input markup.
144+
This defaults to `text/html`.
145+
146+
`output_mime_type`
147+
: A string representing the default output MIME type.
148+
This defaults to `text/x-html-safe`, which is a Plone-specific MIME type that disallows certain tags.
149+
Use the {guilabel}`HTML Filtering` control panel in Plone to control the tags.
150+
151+
`allowed_mime_types`
152+
: A tuple of strings giving a vocabulary of allowed input MIME types.
153+
If this is `None` (the default), the allowable types will be restricted to those set in Plone's {guilabel}`Markup` control panel.
154+
155+
The *default* field can be set to either a Unicode object (in which case it will be assumed to be a string of the default MIME type) or a {ref}`backend-fields-richtextvalue-label`.
156+
157+
158+
#### reStructuredText transformation
159+
160+
Below is an example of a field that allows StructuredText and reStructuredText, transformed to HTML by default.
161+
162+
```python
163+
from plone.app.textfield import RichText
164+
from plone.supermodel import model
165+
166+
defaultBody = """\
167+
Background
168+
==========
169+
170+
Please fill this in
171+
172+
Details
173+
-------
174+
175+
And this
176+
"""
177+
178+
class ITestSchema(model.Schema):
179+
180+
body = RichText(
181+
title="Body text",
182+
default_mime_type='text/x-rst',
183+
output_mime_type='text/x-html',
184+
allowed_mime_types=('text/x-rst', 'text/structured',),
185+
default=defaultBody,
186+
)
187+
```
188+
189+
190+
(backend-fields-richtextvalue-label)=
191+
192+
#### RichTextValue
193+
194+
The `RichText` field, most of the time does not store a string.
195+
Instead, it stores a `RichTextValue` object.
196+
This is an immutable object that has the following properties.
197+
198+
`raw`
199+
: A Unicode string with the original input markup.
200+
201+
`mimeType`
202+
: The MIME type of the original markup, for example `text/html` or `text/structured`.
203+
204+
`encoding`
205+
: The default character encoding used when transforming the input markup.
206+
Most likely, this will be UTF-8.
207+
208+
`raw_encoded`
209+
: The raw input encoded in the given encoding.
210+
211+
`outputMimeType`
212+
: The MIME type of the default output, taken from the field at the time of instantiation.
213+
214+
`output`
215+
: A Unicode object representing the transformed output.
216+
If possible, this is cached persistently, until the `RichTextValue` is replaced with a new one (as happens when an edit form is saved, for example).
217+
218+
The storage of the `RichTextValue` object is optimized for the case where the transformed output will be read frequently (for example, on the view screen of the content object) and the raw value will be read infrequently (for example, on the edit screen).
219+
Because the output value is cached indefinitely, you will need to replace the `RichTextValue` object with a new one if any of the transformation parameters change.
220+
However, as we will see below, it is possible to apply a different transformation on demand, if you need to.
221+
222+
The code snippet below shows how a `RichTextValue` object can be constructed in code.
223+
In this case, we have a raw input string of type `text/plain` that will be transformed to a default output of `text/html`.
224+
Note that we would normally look up the default output type from the field instance.
225+
226+
```python
227+
from plone.app.textfield.value import RichTextValue
228+
229+
context.body = RichTextValue("Some input text", 'text/plain', 'text/html')
230+
```
231+
232+
The standard widget used for a `RichText` field will correctly store this type of object for you, so it is rarely necessary to create one yourself.
233+
234+
235+
236+
237+
127238
### Fields in `plone.schema`
128239

129240
See {ref}`backend-ploneschema-label` for more details.

0 commit comments

Comments
 (0)