Additional editor field in the mod_form

Additional editor field in the mod_form

by Fritz Richter -
Number of replies: 2
Hi,

I would like to have an additional editor field in the mod_form of the Forum module.

I’m aware of this thread, but so far I was not able to get it work.

Here is what I did:

  1. I added the two fields „newtextfield" and „newtextfieldformat" to the database tables.
  2. I added to mod/forum/locallib.php
function forum_get_editor_options($context) {
    global $CFG;
    return array('subdirs'=>1, 'maxbytes'=>$CFG->maxbytes, 'maxfiles'=>-1, 'changeformat'=>1, 'context'=>$context, 'noclean'=>1, 'trusttext'=>0);
}

  1. I added to mod/forum/mod_form.php right at the beginning:
require_once($CFG->dirroot.'/mod/forum/locallib.php’);

  1. I added to mod/forum/mod_form.php:
$mform->addElement('editor', 'newtextfieldeditor', 'newtextfield_label', null, forum_get_editor_options($this->context));

At this point I stuck.

  1. I don’t know, how to change the „data_preprocessing" function in mod/forum/mod_form.php
  2. I don’t know, how to change the „forum_add_instance" function in mod/forum/lib.php
  3. I don’t know, how to change the „forum_update_instance" function in mod/forum/lib.php

I would be very grateful, if someone could guide me through the missing steps.

Fritz
Average of ratings: -
In reply to Fritz Richter

Re: Additional editor field in the mod_form

by Farhan Karmali -
Picture of Core developers Picture of Plugin developers Picture of Testers

Hi,


For the point 2 and 3 for add_instance and update_instance you need to get the text key value from the array of the editor element so basically your code would be something like


$forum->newtextfield    = $forum->newtextfieldeditor['text'];
$forum->newtextfieldformat = $forum->newtextfieldeditor['format'];

I am not very well versed with the preprocessor part, you can read here https://docs.moodle.org/dev/Using_the_File_API_in_Moodle_forms#Replace_old_htmleditor_with_editor

Hope that helps

In reply to Farhan Karmali

Re: Additional editor field in the mod_form

by Fritz Richter -

Hi Farhan,

Thanks for your reply.

As suggested in this post, I tried to use the code in the mod/page activity to add my editor field.

This, however, looks by far more complicated than the two lines proposed in your post. Do you have any further advise?

Here is the relevant code of the add_instance function of the Page activity:

if ($mform) {

        $data->content       = $data->page['text'];

        $data->contentformat = $data->page['format'];

    }

    $data->id = $DB->insert_record('page', $data);

    // we need to use context now, so we need to make sure all needed info is already in db

    $DB->set_field('course_modules', 'instance', $data->id, array('id'=>$cmid));

    $context = context_module::instance($cmid);

    if ($mform and !empty($data->page['itemid'])) {

        $draftitemid = $data->page['itemid'];

        $data->content = file_save_draft_area_files($draftitemid, $context->id, 'mod_page', 'content', 0, page_get_editor_options($context), $data->content);

        $DB->update_record('page', $data);

    }

If I am right, this needs to be added to the add_instance function of the Forum module.