📝 Rich Text Editor in a Form
Have you ever wanted to allow persons/end users to input HTML? You might do this to allow a person lacking a Slate user account to insert content, or for other reasons.
This technique has been lightly tested. Please test thoroughly!
Summary
Basically, we pick a Paragraph Text (textarea) form element, supply some background scripts, then transform the textarea into a CKEditor.
Your text field will store raw HTML. If you output it in a portal, use Liquid like {{ field_name | rawhtml }}
to avoid the HTML being escaped.
Instructions
- Create a Paragraph Text form element. We recommend width 80 and height 10.
- Create an Instructions form element below the Paragraph Text with the below code.
- The first three scripts are needed resources. You may need to find new/updated resource URL's as Technolutions continues to update Slate.
- The last
<script>
tag actually attaches CKEditor to the text box. You must replace form_14002e40-7a52-4002-b282-256efa3cf1ea with the actual ID of the textarea element, which can be found with your browser's inspector tool. removePlugins
removes images and template options under the assumption that people submitting the form do not have /manage access.
<script src="/fw/framework/ckfinder/ckfinder.js?v=25af&cdn=0"></script>
<script src="//slate-technolutions-net.cdn.technolutions.net/manage/deliver/ckeditor.js?v=TS-25af-635119916154460870"></script>
<script src="//fw.cdn.technolutions.net/framework/ckeditor.js?v=25af"></script>
<script>
// Attach the editor
if (CKEDITOR.instances)
if (CKEDITOR.instances['form_14002e40-7a52-4002-b282-256efa3cf1ea'])
CKEDITOR.remove(CKEDITOR.instances['form_14002e40-7a52-4002-b282-256efa3cf1ea']);
editor = CKEDITOR.replace('form_14002e40-7a52-4002-b282-256efa3cf1ea',
{
startupFocus: true,
fullPage: false,
height: 320,
forceEnterMode: true,
toolbar: CKEDITOR.getToolbar('full'),
removePlugins: 'image,templates'
});
</script>
Alternate version that allows image and template browsing:
<script>
// Attach the editor
if (CKEDITOR.instances)
if (CKEDITOR.instances['form_14002e40-7a52-4002-b282-256efa3cf1ea'])
CKEDITOR.remove(CKEDITOR.instances['form_14002e40-7a52-4002-b282-256efa3cf1ea']);
editor = CKEDITOR.replace('form_14002e40-7a52-4002-b282-256efa3cf1ea',
{
filebrowserImageBrowseUrl: '/manage/database/asset?cmd=browse&type=images',
filebrowserBrowseUrl: '/manage/database/asset?cmd=browse&type=documents',
templates_files: ['/manage/deliver/?cmd=templates'],
startupFocus: true,
fullPage: false,
height: 320,
forceEnterMode: true,
toolbar: CKEDITOR.getToolbar('full'),
});
</script>
2 Comments
I propose the following, which allows us to use the ExportKey, which is user defined in the form.
// Define the export key
var exportKey = "purpose";
// Find the textarea based on the export key and get its ID
var textareaId = $('div[data-export="' + exportKey + '"] textarea').attr('id');
// Attach the CKEditor using the dynamically obtained ID
if (textareaId) {
if (CKEDITOR.instances[textareaId]) {
CKEDITOR.remove(CKEDITOR.instances[textareaId]);
}
editor = CKEDITOR.replace(textareaId, {
filebrowserImageBrowseUrl: '/manage/database/asset?cmd=browse&type=images',
filebrowserBrowseUrl: '/manage/database/asset?cmd=browse&type=documents',
templates_files: ['/manage/deliver/?cmd=templates'],
startupFocus: true,
fullPage: false,
height: 320,
forceEnterMode: true,
toolbar: CKEDITOR.getToolbar('full'),
});
}
... and even better
// Define the export key
var exportKey = "purpose";
// Load CKEditor and CKFinder scripts dynamically
$.when(
$.getScript("/fw/framework/ckfinder/ckfinder.js?v=25af&cdn=0"),
$.getScript("//slate-technolutions-net.cdn.technolutions.net/manage/deliver/ckeditor.js?v=TS-25af-635119916154460870"),
$.getScript("//fw.cdn.technolutions.net/framework/ckeditor.js?v=25af")
).done(function() {
// All scripts loaded successfully, now find the textarea and attach CKEditor
var textareaId = $('div[data-export="' + exportKey + '"] textarea').attr('id');
if (textareaId) {
if (CKEDITOR.instances[textareaId]) {
CKEDITOR.remove(CKEDITOR.instances[textareaId]);
}
editor = CKEDITOR.replace(textareaId, {
filebrowserImageBrowseUrl: '/manage/database/asset?cmd=browse&type=images',
filebrowserBrowseUrl: '/manage/database/asset?cmd=browse&type=documents',
templates_files: ['/manage/deliver/?cmd=templates'],
startupFocus: true,
fullPage: false,
height: 320,
forceEnterMode: true,
toolbar: CKEDITOR.getToolbar('full'),
});
}
}).fail(function() {
console.error("One or more scripts failed to load.");
});
-Lloyd