Customize TinyMCE for an Dexterity RichWidget

201 views Asked by At

I'm creating some custom content types using dexterity. I would like to "customize" the aspect of a RichText Field allowing only basic buttons of TinyMce on this field.

In Archetypes I could use

TextField('text',
allowable_content_types=('text/html',),
default_output_type='text/x-html-safe',
required=1,
widget=RichWidget
(label='Content',
    allow_buttons=(
    'bold',
    'italic',
    'justifyleft',
    'justifyright',
    ),
),),

How would I do this with Dexerity based contenttypes?

1

There are 1 answers

3
Travv15 On

There doesn't appear to be a "nice" way to do this right now. Even the Plone docs are currently at a loss. http://docs.plone.org/develop/plone/forms/wysiwyg.html#id9

The problem lies with Products.TinyMCE trying to get the WYSIWYG configuration from the widget attribute of the Field.

https://github.com/plone/Products.TinyMCE/blob/1.3.6/Products/TinyMCE/utility.py#L711-L713

# Get widget attributes
widget = getattr(field, 'widget', None)
filter_buttons = getattr(widget, 'filter_buttons', None)
allow_buttons = getattr(widget, 'allow_buttons', None)

But, as I understand, with Dexterity we instead map fields to widgets on the form object.

from plone.autoform import directives as form
from plone.app.z3cform.wysiwyg import WysiwygFieldWidget

class IExample(model.schema):
    form.widget('body', WysiwygFieldWidget)
    body = schema.Text(title=u"Body")

So, our body field possesses no widget attribute from which Products.TinyMCE can extract configurations.

At any rate, if you need it to work right now, I was able to hack it by doing the following:

In your ZMI, customize portal_skins/tinymce/tinymce_wysiwyg_support to change the line field field|nothing to field field|view/field|nothing.

Define your content type in a schema-driven fashion, and for your WYSIWYG field do the following:

class mywidg(object):
    allow_buttons = ('bold',
                     'italic',
                     'justifyright',
                     'justifyleft',)

class IExample(model.schema):
    form.widget('body', WysiwygFieldWidget)
    body = schema.Text(title=u"Body")
    body.widget = mywidg()