General developer forum

Adding content of editor into database

 
Picture of Binh Le Van Kiem
Adding content of editor into database
 

I'm just start with moodle for a week and I had successed creating a new page like this:
question

I created it with structure like in doc:
https://docs.moodle.org/dev/NEWMODULE_Documentation

But now I stuck at saving the content into database, like completely lost the way how to do that. I had read other plugin such as: mod/page, mod/blog, mod/glossary but it still complicated for beginner like me.
I really appreciate any help of you guys.
Thanks!

 
Average of ratings: -
Picture of Mark Johnson
Re: Adding content of editor into database
Core developersParticularly helpful MoodlersPlugin developers
It's probably useful to read the Formslib usage documentation for the basic workflow of handling a form submission, and the documentation for the editor field type for more specific information about handling that data.
The Data Manipulation API explains how to actually insert and update records in the database.

If you get stuck on a specific part of that process, let us know what you've done and we might be able to point you in the right direction.
 
Average of ratings: Useful (1)
Picture of Binh Le Van Kiem
Re: Adding content of editor into database
 
- I had decied to use your way instead of following the code in mod/page and mod/glossary. And I had succeed in insert it into database - thanks sir.

- And now I'm figuring out the way to show that content into editor when editing and show it to another page.

Once again, thank for your help. I really appreciate that.

 
Average of ratings: -
Picture of Binh Le Van Kiem
Re: Adding content of editor into database
 
Well, i did it in very simple way. 

To appear the content when editing, I put these code in edit page (aboutus_check_content function to check any record exist)

{
$temp = $DB->get_record('aboutus',array('id'=>1));
$mform->set_data(['aboutus' => ['text' => ''.$temp->content , 'format' => FORMAT_HTML]]);
}
And these code in the page I show content in public

if( aboutus_check_content() )
{
$data = $DB->get_record('aboutus',array('id'=>1));
$text = $data->content;

}
echo "".$text;


 
Average of ratings: -
Picture of Mark Johnson
Re: Adding content of editor into database
Core developersParticularly helpful MoodlersPlugin developers

Unless the users of the form you're creating are 100% trusted, you'll probably want to sanitise the text before outputting it, by running it through format_text() or format_string() to mitigate cross-site scripting or otherwise problematic markup.

 
Average of ratings: -