Adding a text editor to a Moodle form

Adding a text editor to a Moodle form

by Maximilian Lutz -
Number of replies: 0

Hi,

I am new in working with moodle and I hope I can get some help here.

I'm trying to add a text editor in the moodle form (mediathek/mod_form.php)

The old code that I had looked like this:

/// Adding the required "summary" field to hold the description of the instance

    $mform->addElement('htmleditor', 'summary', get_string('mediatheksummary', 'mediathek'));
    $mform->setType('summary', PARAM_RAW);
    $mform->addHelpButton('summary', 'mediatheksummary', 'mediathek');
I changed the deprecated htmleditor to editor but since then the changes that I write into the editor wont be saved.

I followed the instructions here.

/// Adding the required "summary" field to hold the description of the instance
$textfieldoptions = array('trusttext'=>true, 'subdirs'=>true, 'maxfiles'=>EDITOR_UNLIMITED_FILES, 'maxbytes'=>null, 'context'=>null);
$mform->addElement('editor', 'summary', get_string('mediatheksummary', 'mediathek'), null, $textfieldoptions);
$mform->setType('summary', PARAM_RAW);
$mform->addHelpButton('summary', 'mediatheksummary', 'mediathek');
$data = file_prepare_standard_editor($data, 'textfield', $textfieldoptions, $context=null, 'mod_mediathek', 'filearea', $itemid=null);
$data = file_postupdate_standard_editor($data, 'textfield', $textfieldoptions, $context, 'mod_mediathek', 'filearea', $itemid=null);
I am aware of the this post and im stuck at the same point but I didn't get it to work.

The same problem seems to be in this here but unfortunatelly that also didn't have any solution.

I also already tried looking into others like mod/page/mod_form.php

I followed 1-3 but like on the other threads im stuck in  the steps 4 and 5.

Where should I put that code? Inside the mod_form.php? Does it have to be inside a particular function (like validation(), data_preprocessing()...)?

What's the "data" variable and where do I get it from?


  1. name database fields: textfield, textfieldformat (and textfieldtrust if required)
  2. create options array. note that context is the best, most local context you have available.
    $textfieldoptions = array('trusttext'=>true, 'subdirs'=>true, 'maxfiles'=>$maxfiles,
    'maxbytes'=>$maxbytes, 'context'=>$context);
  3. add editor textfield_editorto moodle form, pass options through custom data in form constructor, set $data->id to null if data not exist yet
    $mform->addElement('editor', 'textfield_editor', get_string('fieldname', 'somemodule'),
    null, $textfieldoptions);
  4.  prepare data
    $data = file_prepare_standard_editor($data, 'textfield', $textfieldoptions, $context,
    'mod_somemodule', 'somearea', $data->id);
  5. get submitted data and after inserting/updating of data
    $data = file_postupdate_standard_editor($data, 'textfield', $textfieldoptions, $context,
    'mod_somemodule', 'somearea', $data->id);
I would be very grateful, if someone could guide me through the missing steps.

max




Average of ratings: -