4

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.

Waynebird
  • 227
  • 1
  • 8

1 Answers1

4

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.
Community
  • 1
  • 1
Mathias
  • 6,777
  • 2
  • 20
  • 32
  • Thanks for the usefull suggestion. I wanted to test your solution by inserting it into my `richtext.py` in my behaviors folder. It does not seem to take effect. But I am not sure if I am in the correct folder all along. It is a `.egg` folder in `buildout-cache`. Am I on the wrong path? – Waynebird Jul 05 '16 at 11:11
  • @Waynebir: could you post your code online (e.g. github repo)? Besides I suggest you do the training at http://training.plone.org/5 so you can understand where is what. – Davi Lima Jul 05 '16 at 15:49
  • 1
    I updated my answer with some infos about plone.supermodel and another way of implementing a default value for the text field. – Mathias Jul 05 '16 at 16:08