The case of the strange RichText widgets

On a Plone 4.3 with plone.app.widgets 1.8.0 and plone.app.contenttypes 1.1b5 installed, there's one weird bit of inconsistency:

The TinyMCE widget rendered by the plone.app.contenttype's IRichText behaviour is different from any other RichText field added in the dexterity model. Even on the same page, for example, if I edit the Document dexterity type and add a rich text field, the resulting widget is different. How do I know? Try inserting an image by selecting it, in the popup dialog, from the site content browser. In the case of the IRichText text field, it will work, but it will not work for the default text field. So why is that? The IRichText schema could be nothing simpler:

from plone.app.textfield import RichText as RichTextField


@provider(IFormFieldProvider)
class IRichText(model.Schema):

    text = RichTextField(
        title=_(u'Text'),
        description=u"",
        required=False,
    )

The answer is: it's a different widget, not the one coming from plone.app.textfield. plone.app.widgets replaces the widget for the IRichText.text field:

(see dx_bbb.py):

from plone.app.widgets.dx import RichTextWidget

try:
    from plone.app.contenttypes.behaviors.collection import ICollection
    from plone.app.contenttypes.behaviors.richtext import IRichText
    HAS_PAC = True
except ImportError:
    HAS_PAC = False
...
if HAS_PAC:
...
    @adapter(getSpecification(IRichText['text']), IWidgetsLayer)
    @implementer(IFieldWidget)
    def RichTextFieldWidget(field, request):
        return FieldWidget(field, RichTextWidget(request))

And this is the code that actually fixes the relatedItems popup widget that enables embeding images in the text (from plone.app.widgets.utils.get_tinymce_options ):

        args['pattern_options'] = {
            'relatedItems': {
                'vocabularyUrl': '{0}/{1}'.format(
                    config['portal_url'],
                    '@@getVocabulary?name=plone.app.vocabularies.Catalog'
                ),
                'mode': 'browse',
                'basePath': folder_path,
                'folderTypes': utility.containsobjects.split('\n')
            },
            'upload': {
                'initialFolder': initial,
                'currentPath': folder_url_relative,
                'baseUrl': config['document_base_url'],
                'relativePath': '@@fileUpload',
                'uploadMultiple': False,
                'maxFiles': 1,
                'showTitle': False
            },
            'tiny': config,
            # This is for loading the languages on tinymce
            'loadingBaseUrl': '++resource++plone.app.widgets.tinymce',
            'prependToUrl': 'resolveuid/',
            'linkAttribute': 'UID',
            'prependToScalePart': '/@@images/image/',
            'folderTypes': utility.containsobjects.split('\n'),
            'imageTypes': utility.imageobjects.split('\n'),
            'anchorSelector': utility.anchor_selector,
            'linkableTypes': utility.linkable.split('\n')
        }
The relatedItems options is not filled in in the TinyMCE widget rendered directly by plone.app.textfield. 
 
So, in my case, the fix is to register an adapter:
    Fix RichText widget: the plone.app.widgets provided one has proper relatedItems configuration
    <adapter
        for="plone.app.textfield.interfaces.IRichText mypackage.IMyLayer"
        factory="plone.app.widgets.dx.RichTextFieldWidget"
        />
Which only exposes a bug in collective.cover / plone.app.tiles, due to the way get_tinymce_options works. Oh well, moving on...

Comments