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
+187Lines changed: 187 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -103,6 +103,25 @@ See [`plone.namedfile`](https://pypi.org/project/plone.namedfile/) and [plone.fo
103
103
|`NamedBlobFile`|`NamedBlobFile`| A binary uploaded file stored as a ZODB blob. Requires the `blobs` extra to `plone.namedfile`. Otherwise identical to `NamedFile`. |`IField`|
104
104
|`NamedBlobImage`|`NamedBlobImage`| A binary uploaded image stored as a ZODB blob. Requires the `blobs` extra to `plone.namedfile`. Otherwise identical to `NamedImage`. |`IField`|
105
105
106
+
#### `NamedBlobImage`
107
+
108
+
The following example shows how to create an `Image` object, and attach the image file to it.
109
+
110
+
```python
111
+
img_obj = api.content.create(
112
+
container=ww_article,
113
+
type="Image",
114
+
id="test.jpg",
115
+
image=NamedBlobImage(
116
+
data=img_file,
117
+
filename="test.jpg",
118
+
)
119
+
)
120
+
```
121
+
122
+
123
+
124
+
106
125
107
126
### Fields in `z3c.relationfield.schema`
108
127
@@ -115,6 +134,8 @@ See [`z3c.relationfield`](https://pypi.org/project/z3c.relationfield/) for more
115
134
|`RelationChoice`|`RelationValue`| A `Choice` field intended to store `RelationValue`s | See {ref}`Choice <fields-in-zope-schema-label>`|
116
135
117
136
137
+
(backend-fields-richtext-label)=
138
+
118
139
### Fields in `plone.app.textfield`
119
140
120
141
See [`plone.app.textfield`](https://pypi.org/project/plone.app.textfield/) for more details.
@@ -124,6 +145,172 @@ See [`plone.app.textfield`](https://pypi.org/project/plone.app.textfield/) for m
124
145
|`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
146
126
147
148
+
The `RichText` field allows for alternative markups and content filtering.
149
+
The following code sample shows how to create a schema with a `RichText` field.
150
+
151
+
```python
152
+
from plone.app.textfield import RichText
153
+
from plone.supermodel import model
154
+
155
+
classITestSchema(model.Schema):
156
+
157
+
body = RichText(title="Body text")
158
+
```
159
+
160
+
The `RichText` field constructor can take the following arguments, in addition to the usual arguments for a `Text` field.
161
+
162
+
`default_mime_type`
163
+
: A string representing the default MIME type of the input markup.
164
+
This defaults to `text/html`.
165
+
166
+
`output_mime_type`
167
+
: A string representing the default output MIME type.
168
+
This defaults to `text/x-html-safe`, which is a Plone-specific MIME type that disallows certain tags.
169
+
Use the {guilabel}`HTML Filtering` control panel in Plone to control the tags.
170
+
171
+
`allowed_mime_types`
172
+
: A tuple of strings giving a vocabulary of allowed input MIME types.
173
+
If this is `None` (the default), the allowable types will be restricted to those set in Plone's {guilabel}`Markup` control panel.
174
+
175
+
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`.
176
+
177
+
178
+
#### reStructuredText transformation
179
+
180
+
Below is an example of a field that allows StructuredText and reStructuredText, transformed to HTML by default.
The `RichText` field usually does not store a string.
215
+
Instead, it stores a `RichTextValue` object.
216
+
This is an immutable object that has the following properties.
217
+
218
+
`raw`
219
+
: A Unicode string with the original input markup.
220
+
221
+
`mimeType`
222
+
: The MIME type of the original markup, for example, `text/html` or `text/structured`.
223
+
224
+
`encoding`
225
+
: The default character encoding used when transforming the input markup.
226
+
Most likely, this will be UTF-8.
227
+
228
+
`raw_encoded`
229
+
: The raw input encoded in the given encoding.
230
+
231
+
`outputMimeType`
232
+
: The MIME type of the default output, taken from the field at the time of instantiation.
233
+
234
+
`output`
235
+
: A Unicode object representing the transformed output.
236
+
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).
237
+
238
+
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).
239
+
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.
240
+
However, as we will see below, it is possible to apply a different transformation on demand, if you need to.
241
+
242
+
The code snippet below shows how a `RichTextValue` object can be constructed in code.
243
+
In this case, we have a raw input string of type `text/plain` that will be transformed to a default output of `text/html`.
244
+
Note that we would normally look up the default output type from the field instance.
245
+
246
+
```python
247
+
from plone.app.textfield.value import RichTextValue
This operation is approximately as efficient as rendering a simple `Text` field, since the transformation is only applied once, when the value is first saved.
278
+
279
+
280
+
##### Alternative transformations
281
+
282
+
Sometimes, you may want to invoke alternative transformations.
283
+
Under the hood, the default implementation uses the `portal_transforms` tool to calculate a transform chain from the raw value's input MIME type to the desired output MIME type.
284
+
If you need to write your own transforms, take a look at [Changing Portal Transforms Settings via Python](https://5.docs.plone.org/develop/plone/misc/portal_transforms.html).
285
+
This is abstracted behind an `ITransformer` adapter to allow alternative implementations.
286
+
287
+
To invoke a transformation in code, you can use the following syntax.
288
+
289
+
```python
290
+
from plone.app.textfield.interfaces import ITransformer
0 commit comments