Warning

This document hasn't been checked for compatibility with current versions of Plone. Use at your own risk.

Using multiple rich text fields in a custom AT content type

by Sverre Andreas Dannbolt last modified Dec 30, 2008 03:03 PM
This howto explains how to find the format of a specific rich text field in a custom AT content type. If you have more than one field, the standard way of doing this (which is based on the rich text field being the «primary» field) doesn't work. The howto also explains how to set the default output type of a rich text field.

The standard way of displaying the text of a rich text field is like this (from Plone's document_view.pt):

<div class="stx"
     tal:condition="text"
     tal:attributes="class python:test(here.Format() in ('text/structured',
                                       'text/x-rst', ), 'stx', 'plain')">
  <div tal:replace="structure text" />
</div>

Getting the format of the field

Now, here.Format() is actually just the format of the primary field. If you have several rich text fields you don't have a primary field, so this wont do much good. As a result, you need to use the method getContentType(). getContentType() is defined in Products/Archetypes/BaseObject.py, but all you need is to do is to use the following code instead of the standard code:

<div class="stx"
     tal:condition="text"
     tal:attributes="class python:test(here.getContentType('fieldname') in ('text/structured',
                                       'text/x-rst', ), 'stx', 'plain')">
  <div tal:replace="structure text" />
</div>

(Remember to replace 'fieldname' with the name of your field.)

As you can see, all that is changed is swapping the here.Format() with here.getContentType('fieldname')

Getting the content of the field

Also, you have to define the variable text. In document_view.pt, text is defined like this:

tal:define="text python: here.CookedBody(stx_level=2)"

As you don't have a body in your content type, but rather several rich text fields, this CookedBody method wont work (If your first thought is «Hey, I'll just write "text here/fieldname"», you shouldn't. You should never use here/fieldname references at all!). What you do is use the getFieldname() method (remember, again, to replace 'Fieldname' with your field name :-). If the rich text field is called summary, for example, then you would define text like this:

tal:define="text python: here.getSummary()"

If this works, then the Schema of your content type is well written. If it doesn't work, it is probably because getSummery() doesn't know the wanted output type of the data that is returned.

Changing the output format

You can change the output format manually by replacing the definition above with this:

tal:define="text python: here.getSummary('text/x-html-safe')"

By adding 'text/x-html-safe', you tell getSummary what kind of output type you want the data to be returned as. This should return the text of the summary rich text field «cooked» (read: baked, barbecued and boiled into something that is readable to the users web browser, like html).

If this works, you should make this the default output type by adding default_output_type to the TextField in your AT Schema.

Setting the default output type

This is an example of a Summary TextField part of the Schema:

TextField(
    name='summary',
    default_content_type = 'text/restructured',
    default_output_type = 'text/x-html-safe',
    allowable_content_types=('text/plain', 'text/restructured', 'text/html',),
    widget=RichWidget(
        label="Summary",
        description="A synopsis, précis, résumé, abstract, digest, encapsulation, abbreviated version, outline, sketch, rundown, review, summing-up, overview, recapitulation, epitome.",
        rows="10",
    ),
    searchable=1,
    required=0
),

As you can see, I've added the line:

default_output_type = 'text/x-html-safe',

Now getSummary() know that it should return the content of the rich text field as text/x-html-safe, and not as plain text. And getSummary() knows this even without telling it with a parameter, like we did above.

Learn more

To learn more about the default_output_type and other transforms, you can read Martin Aspelis tutorial Converting text with Portal Transforms and the MIME Types Registry.


Contribute

Something wrong or out of date? Anybody can edit or create a new article in the knowledge base. Simply create an account on this site, log in, and click the Edit button to contribute.