General developer forum

Add editor in local plugin

Picture of Amin Farajzadeh
Add editor in local plugin


I added an editor in moodle form based on this documentation : Using_the_File_API_in_Moodle_forms#editor

It works fine in inserting data.

when I want to update it , it shows me broken image for images


$mform->addElement('editor', 'description_editor', get_string('description'), null, local_nadopages_get_editor_options());

editpage.php :

$page = $DB->get_record('nadopages', ['id' => $id]);
$data = file_prepare_standard_editor($page, 'description', local_nadopages_get_editor_options(), $context, 'local_nadopages', 'somearea', $page->id);

on insert/ update:
function local_nadopages_savepage($context, $data, $id = null) {
global $DB;

if ($id) {
$data = file_postupdate_standard_editor($data, 'description', local_nadopages_get_editor_options(), $context, 'local_nadopages', 'somearea', $data->id);
$DB->update_record('nadopages', $data);
} else {
$id = $DB->insert_record('nadopages', $data);
editor options:
function local_nadopages_get_editor_options() {
global $CFG;
return array('subdirs'=>1, 'maxbytes'=>$CFG->maxbytes, 'maxfiles'=>-1, 'changeformat'=>1, 'context'=>context_system::instance(), 'noclean'=>1, 'trusttext'=>0);

Average of ratings: -
Re: Add editor in local plugin
Core developersParticularly helpful MoodlersPlugin developers

When you first insert the record, you're missing a call to file_postupdate_standard_editor().

If you do not have an 'id', you need to call 'insert_record' to create the record and get the id, then you need to call file_postupdate_standard_editor() to copy the draft files into the area referenced by this id and to rewrite the references to the file in the text returned by the editor. Finally, you need to call 'update_record' to save the changed text (you could probably get away with just 'set_field' at this point, but it is simpler to call update_record).

Unfortunately, when creating a new record, there is no way to process the embedded files without two DB updates (insert + get id, fix the files, update).

Average of ratings: Useful (1)
Richard Jones
Re: Add editor in local plugin
Particularly helpful MoodlersPlugin developersTesters

Something like this:

public static function add_page_record($data, $context) {
        global $DB;
        $pagecontentsoptions = simplelesson_get_editor_options($context);
        // Insert a dummy record and get the id.
        $data->timecreated = time();
        $data->timemodified = time();
        $data->pagecontents = ' ';
        $data->pagecontentsformat = FORMAT_HTML;
        $dataid = $DB->insert_record('simplelesson_pages', $data);
        $data->id = $dataid;
        // Massage the data into a form for saving.
        $data = file_postupdate_standard_editor(
        // Update the record with full editor data.
        $DB->update_record('simplelesson_pages', $data);
        return $data->id;

pagecontents is the editor area on the relevant form and 'pagecontents' is the file area declared in lib.php:

function simplelesson_get_file_areas($course, $cm, $context) {
    return array('pagecontents' => 'for page files editor content');

Average of ratings: Useful (1)