Adding a text editor to a Moodle form

Adding a text editor to a Moodle form

by Luis de la Torre -
Number of replies: 7

Hi,

I'd like to add a text editor (apart from the intro, which is easy to add by using the add_intro_editor() function) in the moodle form (mod_form.php) of my activity. When an instance of that activity is added and a user visualizes it, the view.php would show the content stored there when the instance was created.


The instructions from here, explain:

editor

There are two ways of using the editor element in code, the first one is easier but expects some standardized fields.

Simple use

  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 had no problem following steps 1 to 3. That is already done.

However, Im stucked in 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?

Some help would be realy, really appreciated. Im totally lost here sad

Average of ratings: -
In reply to Luis de la Torre

Re: Adding a text editor to a Moodle form

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Step 4 will need to be done in a 'data_preprocessing' function inside your mod_xx_mod_form in mod_form.php - see mod/page/mod_form.php for an example.

Step 5 will need to go in you xx_add_instance and xx_update_instance functions in lib.php - again, see mod/page/lib.php for an example.

To put it in context - the reasons for needing these functions is to handle any files that are attached to the text area during editing (e.g. in inserted image). During editing these are stored in a draft files area (so that clicking on 'cancel' is able to throw away any files that the user has now cancelled the inclusion of and keep any files that would have been thrown away by clicking 'save').

Before you show the editor, the draft files area needs to be filled up with the current files (which is why the page module does not need to do anything if $this->current->instance is undefined - when creating a new page, there are never any current files to load into the draft area). This is done by file_prepare_standard_editor (which also adjusts the links already embedded in the text to serve files from the draft area). When saving the changes the draft and real file areas need to be synchronised (by copying / deleting files as appropriate) and the links embedded in the text need to be adjusted ready for saving in the database. This is done by file_postupdate_standard_editor.

Average of ratings: Useful (5)
In reply to Davo Smith

Re: Adding a text editor to a Moodle form

by Luis de la Torre -

Davo,

Thanks SO MUCH for such a detailed reply. That really helped me.

Especially, the example you refered me at (the mod/page activity).

I previously had a look at the glossary activity trying to find some clues about this issue but it looked just too complicated to me. This example is much easier!

I'll try to make things work.

Again, thanks a lot!

In reply to Davo Smith

Re: Adding a text editor to a Moodle form

by Luis de la Torre -

SOLVED!


Tanks again, Davo. You really helped me.

Anyone trying to add an additional text editor to its mod_form should look at the code in the mod/page activity.

It is much easier to do it following that code than just using the help in http://docs.moodle.org/dev/Using_the_File_API_in_Moodle_forms#editor

In reply to Luis de la Torre

Re: Adding a text editor to a Moodle form

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Maybe you could add a note to that effect somewhere suitable on the docs page - with either a link to this discussion or a link to the relevant PHP files in git.moodle.org ?

Possibly it could go in the 'see also' section at the bottom of the page?

In reply to Davo Smith

Re: Adding a text editor to a Moodle form

by Sankar Mahadevan -

Hi Davo,

We have developed a custom module called My Assessments.  Admin can create an assessment; while creating he can create input fields like textarea, checkbox, radio button for user inputs.  All these input field information will be stored in database.  User can log in and give their answers in input fileds like textarea, checkbox, radio button.  User can edit the entered input value also.  Both the form and input fields will be created dynamically with the help of information stored in database.  I am able to create a texteditor in other places with moodle media by adding the code "$mform->addElement('editor', 'introeditor', get_string('intro', 'assess'), null, array('maxfiles' => EDITOR_UNLIMITED_FILES));" . But here I dont know how to create texteditor with moodle media.  Here we dont use $mform also.  Please help me to enable this.  Any help would be appreciated.  Thanks in advance.

Regards,

Sankar.