Rich text in controlpanel [SOLVED, kind of]

How can I use rich text in control panel, plone 6?

Before, I have done this with

 myfield = RichText(

But in Plone 6 (Classic), I end up with error (See below). I dont have (or need) any template to display the field, so how can make this field pass validation?

zope.schema._bootstrapinterfaces.WrongType: (RichTextValue object. (Did you mean <attribute>.raw or <attribute>.output?), <class 'str'>, 'value')

Can you show the full field definition and the full traceback?

Do you have plone.volto installed?

plone.volto disables the Richtext and Table of Contents behaviors for the Document content type. (See plone.volto/t/README.rst)

No, it is a 'plain classic'.

UPDATE: I installed volto (too) just to test, error is the same

This is what I have (now):

  Traceback (innermost last):
    Module ZPublisher.WSGIPublisher, line 181, in transaction_pubevents
    Module ZPublisher.WSGIPublisher, line 390, in publish_module
    Module ZPublisher.WSGIPublisher, line 285, in publish
    Module ZPublisher.mapply, line 85, in mapply
    Module ZPublisher.WSGIPublisher, line 68, in call_object
    Module plone.z3cform.layout, line 61, in __call__
    Module plone.z3cform.layout, line 45, in update
    Module plone.z3cform.fieldsets.extensible, line 62, in update
    Module plone.z3cform.patch, line 31, in GroupForm_update
    Module z3c.form.group, line 145, in update
    Module plone.app.z3cform.csrf, line 21, in execute
    Module z3c.form.action, line 98, in execute
    Module z3c.form.button, line 301, in __call__
    Module z3c.form.button, line 159, in __call__
    Module plone.app.registry.browser.controlpanel, line 60, in handleSave
    Module z3c.form.group, line 116, in applyChanges
    Module z3c.form.group, line 82, in applyChanges
    Module z3c.form.form, line 51, in applyChanges
    Module z3c.form.datamanager, line 91, in set
    Module medialog.controlpanel.controlpanel, line 32, in __setattr__
    Module plone.registry.recordsproxy, line 48, in __setattr__
    Module plone.registry.registry, line 45, in __setitem__
    Module plone.registry.record, line 79, in _set_value
    Module zope.schema._bootstrapfields, line 295, in validate
    Module zope.schema._bootstrapfields, line 510, in _validate
    Module zope.schema._bootstrapfields, line 344, in _validate
  zope.schema._bootstrapinterfaces.WrongType: (RichTextValue object. (Did you mean <attribute>.raw or <attribute>.output?), <class 'str'>, 'value')

Can you show your code?

Make sure the MIME type of the value is in the allowed_mime_types property. (See TextField Validation)

I have used this code 'before', I am pretty sure it has something to do with the 'upgrade to bootstrapfields'. Usually, this is the error you get in a template if you try to use context/field insted of context/field/output.

class IDocentimsSettings(model.Schema):
"""Adds settings to controlpanel

project_description = RichText(
    title=u"Project Description Rich text",
    required=False, 
)

The error says that you are trying to store a RichTextValue object in a field that expects a Python str.

My guess is that the registry still has an old field definition stored persistently, and is not using a field of type RichText. Remember that changing the schema does not immediately update the registry, you have to run a GenericSetup import step to update the persistent schema in the registry from the interface.

I believe it is not possible to store rich text in the plone registry. From the plone.registry readme:

"plone.registry only supports certain fields, and disallows use of a few properties even of those. As a rule of thumb, so long as a field stores a Python primitive, it is supported; the same goes for attributes of fields."

Check the list of supported fields: plone.registry · PyPI

Fields in the registry need to be serializable to and from XML and this is not currently possible with rich text field values.

There is no Field for the Type RichTextValue plone.registry · PyPI

Thanks a lot, did not think of that.

I have hacked around it by using pat-tinymce, hope that is good enough:

widget("project_description", klass="pat-tinymce")
project_description = schema.Text(
    required = False,
    title=_(u"label_project_description", default=u"Project Description"),
    description=_(u"help_project_description",
                  default=u"Project description")
    )
1 Like

Came across this because I need it too. You can of course only add the css class to the Text Field, but you loose all the pattern settings, so you have an unstyled content and cannot add internal images or links.

I've solved it like this for me:

from plone.app.registry.browser.controlpanel import RegistryEditForm
from plone.app.textfield import RichText
from plone.app.z3cform.widgets.richtext import RichTextFieldWidget
from plone.autoform import directives
from plone.registry.field import PersistentField
from zope.interface import Interface


class RichTextFieldRegistry(PersistentField, RichText):
    """ persistent registry textfield """


class IMyControlPanel(Interface):
    richtext_controlpanel_field = RichTextFieldRegistry(
        title="richtext registry field",
        required=False,
    )
    directives.widget(richtext_controlpanel_field=RichTextFieldWidget)
    

class MyControlPanel(RegistryEditForm):
    schema = IMyControlPanel

now this richtext field has all the properties set.

6 Likes

Thanks.
I did not even know that the Image (button) did not work (since it was just for rich text).

I changed my control panel field to your code, and it works, thanks

Slighly off topic, but do you have any idea if/how this could be used in DataGridField ( I did a quick test and it give error ( Module collective.z3cform.datagridfield.row, line 96, in toWidgetValue ) .

datagridfield already has a registry implementation.

see collective.z3cform.datagridfield/src/collective/z3cform/datagridfield/registry.py at master · collective/collective.z3cform.datagridfield · GitHub

from collective.z3cform.datagridfield.registry import DictRow

Yes, I already have that, but DictRow has problems with RichTextFields and Images

I notice a strange behavior.
If I add this field to a fieldset, it is not shown in THAT fieldset after install (but in the 'Default'). If I restart the site, it 'moves to the right fieldset'.

This is caching somewhere.

The strang thing is: It only happens if I use a rich text field

When I used the 'workaround appoach (see below), it does not happen.
PS: I add a completely new site, install the add on, go to control panel (where it is in wrong fieldset), stop the site, restart, go to control panel and it is OK.

Works:

widget("project_description", klass="pat-tinymce")
project_description = schema.Text(

Starts with 'wrong fieldset':

widget(project_description=RichTextFieldWidget)
project_description = RichTextFieldRegistry(

It works but if the product is reinstalled, the field is blanked. I mean, I see it empty.

But if I restart the instance, the field value is back. So, If I uninstall/reinstall the product, I've to restart the instance to get richtextfield back.

Very strange.

I'm using:

@provider(IFormFieldProvider)
class IMyControlPanel(model.Schema, Interface)

but the same happen if I remove Interface or @provider(IFormFieldProvider):

class IMyControlPanel(model.Schema)