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/fields.md
+111Lines changed: 111 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -115,6 +115,8 @@ See [`z3c.relationfield`](https://pypi.org/project/z3c.relationfield/) for more
115
115
|`RelationChoice`|`RelationValue`| A `Choice` field intended to store `RelationValue`s | See {ref}`Choice <fields-in-zope-schema-label>`|
116
116
117
117
118
+
(backend-fields-richtext-label)=
119
+
118
120
### Fields in `plone.app.textfield`
119
121
120
122
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
124
126
|`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`|
125
127
126
128
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
+
classITestSchema(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.
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
0 commit comments