Deepu Sasidharan
Deepu Sasidharan

Reputation: 5309

How can I use tinymce editor outside moodle form?

How can I use tinymce editor outside moodle form? In moodle form I can use editor(tinymce) as:

$mform->addElement('editor', 'title', 'Question');
$mform->addRule('title', null, 'required', null, 'client');
$mform->setType('title', PARAM_RAW);

But I want to use the editor field(tinymce) outside moodle form and fetch the value in it.

How can I do this? Please help me...

My moodle version is 2.9.1

Upvotes: 0

Views: 985

Answers (1)

CMR
CMR

Reputation: 1416

It's a bit of an awkward one, as Moodle doesn't really want you to use these things outside of their form classes.

But you can do:

$editor = \editors_get_preferred_editor(); // This gets the default editor for your site
$editor->use_editor("someidhere"); // This is used to set the id of the html element

// This creates the html element
echo \html_writer::tag('textarea', 'somedefaultvalue',
                array('id' => "someidhere", 'name' => 'somenamehere', 'rows' => 5, 'cols' => 10));

That will echo out a textarea element with the "id", "name", etc... you set and should apply the rich text editor to the element. To get the value out of that, you would just treat it like a normal form element, so if you submitted it through a POST request or something, you would just reference it through it's "name".

Here is a very basic example of a script that creates that element and shows you the contents when you submit the form:

<?php

require_once '../../config.php';
require_once $CFG->dirroot.'/lib/form/editor.php';
require_once $CFG->dirroot . '/lib/editorlib.php';

// Set up PAGE
$PAGE->set_context( context_course::instance(1) );
$PAGE->set_url($CFG->wwwroot . '/test.php');
$PAGE->set_title( 'title');
$PAGE->set_heading( 'heading' );

echo $OUTPUT->header();

var_dump($_POST);

$editor = \editors_get_preferred_editor();
$editor->use_editor("someid");


echo "<form method='post'>";
echo \html_writer::tag('textarea', 'default',
    array('id' => "someid", 'name' => 'somename', 'rows' => 5, 'cols' => 10));

echo "<input type='submit' name='submit' />";
echo "</form>";

echo $PAGE->requires->get_end_code();                    

Upvotes: 2

Related Questions