Maybe I have not understand how entries work but I would like to create a rich text field in my plugin without having an entry.

All I want is creating a rich text field with twig but how can I do this? I already read this https://craftcms.com/docs/rich-text-fields but it was not really helpful. I would just like to have something like this just for a rich text field instead of a normal textfield

{{ forms.textField({
    label: 'Ingredient Name'|t,
    required: true,
    name: 'ingredient[name]',
    value: ingredient ? ingredient.name : null,
    errors: ingredient ? ingredient.errors('name') : null,
 }) }}

Could someone please help me?

if I make {{ entry.richTextFieldHandle }} I get the error Variable "entry" does not exist and I don't know how to define entry since I don't use an entry. Do I have to include my own redactor.js if I just want to have an html editor?


If you want a rich text field on a front end - this question has been answered in detail here: How do I enable redactor rich text editor in a front-end form?

In short technically possible but not recommended.

If you want a rich text field for a back end, i.e. plugin, form:

The craft macros for form fields live in craft/app/templates/forms - however I do not believe there is a macro for a rich text field. There are text and textarea macros you can use.

However, I think it is possible in a plugin - this apparently creates a rich text field widget in your craft dashboard, so you should be able to pinch the relevant code from: https://github.com/gilleard/RichTextWidget/blob/master/richtextwidget/widgets/RichTextWidget_DashboardWidget.php

(Specifically look at public function getSettingsHtml() I think)

  • Well I don't want to have it as a front end resource I just don't really understand how to use it in my backend since I don't have the variable entry or another variable similar to it. But thank you. If craft is unable to create a rich text editor itself, I'll use this method Nov 18 '16 at 7:07
  • Oh sorry, my mistake, I missed the plugin bit. It should be fine in that context. I'm away from keyboard now but I'll look through the form lib later Nov 18 '16 at 7:13
  • I've revised my answer above ... there's no out of the box macro for you, I think, but it looks like it is possible - see the plugin I have linked above. Nov 19 '16 at 8:03

Your Answer

