Waynebird
Waynebird

Reputation: 227

Add default content to tinyMCE in Plone 5.0

I am working on a Plone 5.0 setup and I want to modify it in a way, so that users, who wants to create a new page, have a default text in their TinyMCE editor.

I am quite new to Plone and a bit overwhelmed by the amount of different languages and how they interconnect with each other. So instead of working on some files quick and dirty, I would like to have some suggestions how to start working on the problem properly and efficient.

Any suggestions, how to work on that are welcome.

Upvotes: 4

Views: 168

Answers (1)

Mathias
Mathias

Reputation: 6839

The front-end (in this case TinyMCE) is not responsible for the default value, it's the form beneath.

Plone 5 uses Dexterity types with z3c forms.


EDIT: This is how your doing this the old school way - I mean the plone directives way - Sry for misleading you. I still use plone.directives, which supports this kind of default value adapter.

The default content type Document of plone.app.contenttypes is using plone.supermodel. This has a different concept.

If you are still willing to create your own Richtext behavior you may follow those instructions: http://docs.plone.org/external/plone.app.dexterity/docs/advanced/defaults.html

In your case:

def richtext_default_value(**kwargs):
    return RichTextValue('<p>Some text</p>')


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

    text = RichTextField(
        title=_(u'Text'),
        description=u"",
        required=False,
        defaultFactory=richtext_default_value,
    )
    model.primary('text')

You can add a defaultFactory to the text field.

If you hack those lines on your egg, it will work.


Here's some information about setting a default value programmatically:

So in your case this may look something like this:

from plone.directives.form import default_value
from plone.app.contenttypes.behaviors.richtext import IRichText
from plone.app.textfield.value import RichTextValue

@default_value(field = IRichText['text'])
def richtext_default_value(data):
    return RichTextValue('<p>Some text</p>')

You may extend the default_value decorator by context parameter to be more specific: @default_value(field = IRichText['text'], context=my.package.content.interfaces.IMyType)

BUT since we have the concept of behaviors it's may be better to implement your own Richtext behavior with a default value:

  1. Creating a behavior --> http://docs.plone.org/external/plone.app.dexterity/docs/behaviors/creating-and-registering-behaviors.html AND the plone default richtext behavior as template for your own -> https://github.com/plone/plone.app.contenttypes/blob/1.2.16/plone/app/contenttypes/behaviors/richtext.py
  2. Remove the ´plone.app.contenttypes.behaviors.richtext.IRichText` Behavior from your content type (Document) thru ZMI (portal_types -> Document)
  3. Add your own Richtext behavior, this may be something like my.package.behaviors.richtext.IRichtextWithDefaultValue.

Upvotes: 4

Related Questions