Adding the WYSIWYG editor field to a form - mkadin/sort GitHub Wiki

Here are the moodle docs: http://docs.moodle.org/dev/Using_the_File_API_in_Moodle_forms#editor

Here's what was required to add the field to the notepad:

  1. In the table where you are going to store the content, add the fields -- xxxx -- xxxxformat -- xxxxformattrust

for the notepad, these were textfield, textfieldformat, and textfieldtrust

  1. Add the editor field to the form. Use xxxx_editor as the field name.

For the notebook, the following was inserted into notepad_edit_form

$mform->addElement('editor','textfield_editor', 'notepad html editor' ,null,array('maxfiles'=> EDITOR_UNLIMITED_FILES));

there are other options that can be included in the options array.

To get the editor to be in "HTML Format" by default, the field textfieldformat has to have value 1 ('HTML_FORMAT')

  1. In the page that processes the form (in the notebook case it is "session.php"), before adding the form call file_prepare_standard_editor and update the database with the new values and refetch the entire entry. For the notebook, this is:

$session = file_prepare_standard_editor($session, 'textfield', $definitionoptions, $context, 'mod_notepad', 'notepad', $session->id);

// store the updated value values $DB->update_record('notepad_sessions', $session);

//refetch complete entry $session = $DB->get_record('notepad_sessions', array('id'=>$session->id));

  1. When processing the data (in if ($responses = $mform->get_data())) call file_postupdate_standard_editor, and update the database:

// save and relink embedded images and save attachments $responses = file_postupdate_standard_editor($responses, 'textfield', $definitionoptions, $context, 'mod_notepad', 'notepad', $responses->id);

// store the updated value values $DB->update_record('notepad_sessions', $responses);

  1. When setting the existing data you need to link the draft itemid to the form field.

In the notebook: $draftid_editor = file_get_submitted_draft_itemid('textfield_editor'); $currenttext = file_prepare_draft_area($draftid_editor,$context->id,'mod_notepad','notepad', $session->id,array('subdirs'=>true),$session->textfield);

$form_data['textfield_editor'] = array('text'=>$currenttext,'format'=>$session->textfieldformat,'itemid'=>$draftid_editor); $mform->set_data($form_data);

  1. To print the html field you need to convert the urls to absolute and write a lib function to output files:

// get the reflection field for the session $reflections = file_rewrite_pluginfile_urls($session->textfield, 'pluginfile.php', $context->id, 'mod_notepad', 'notepad', $session->id);

function notepad_pluginfile($course, $cm, $context, $filearea, array $args, $forcedownload, array $options=array())

and also put the text through "format_text" function:

// we need to do all this to get Dragmath to work. And it does! $formatoptions = new stdClass; $formatoptions->noclean = true; $formatoptions->overflowdiv = true; $formatoptions->context = $context; $reflections = format_text($reflections, FORMAT_HTML, $formatoptions); echo "

" . $reflections . "
";
⚠️ **GitHub.com Fallback** ⚠️