Note:

If you want to create a new page for developers, you should create it on the Moodle Developer Resource site.

Editor API: Difference between revisions

From MoodleDocs
m (Text replacement - "</code>" to "</syntaxhighlight>")
m (Text replacement - "<code php>" to "<syntaxhighlight lang="php">")
Line 14: Line 14:
For example, assuming there is an HTML text area with id mytextareaid:
For example, assuming there is an HTML text area with id mytextareaid:


<code php>
<syntaxhighlight lang="php">
$editor = editors_get_preferred_editor(FORMAT_HTML);
$editor = editors_get_preferred_editor(FORMAT_HTML);
$editor->use_editor('mytextareaid');
$editor->use_editor('mytextareaid');
Line 35: Line 35:
The following code will cause atto to show the four buttons indicated.
The following code will cause atto to show the four buttons indicated.


  <code php>
  <syntaxhighlight lang="php">
$attobuttons = 'style1 = bold, italic'. PHP_EOL .'list = unorderedlist, orderedlist';
$attobuttons = 'style1 = bold, italic'. PHP_EOL .'list = unorderedlist, orderedlist';
$editor->use_editor($id, ['context' => $context, 'autosave' => false, 'atto:toolbar' => $attobuttons],
$editor->use_editor($id, ['context' => $context, 'autosave' => false, 'atto:toolbar' => $attobuttons],
                 ['return_types' => FILE_EXTERNAL]);</syntaxhighlight>
                 ['return_types' => FILE_EXTERNAL]);</syntaxhighlight>

Revision as of 13:30, 14 July 2021

The editor API lets you control Moodle text editors. It can be found in lib/editorlib.php.

Normally you do not need to use this API directly because you can include editors as part of a Moodle form, which will automatically set up the editor for you.

This documentation is (very) incomplete.

How to set up a text editor

To set up a text editor on an existing HTML text area field:

  • Call function editors_get_preferred_editor(), which will return an object of the texteditor class.
  • Call function use_editor to enable the editor for the text area.

For example, assuming there is an HTML text area with id mytextareaid:

$editor = editors_get_preferred_editor(FORMAT_HTML);
$editor->use_editor('mytextareaid');

Editor options

The use_editor function allows an options array to be supplied.

General options

context
set to the current context object
enable_filemanagement
set false to get rid of the managefiles plugin
autosave
set false to turn off autosave

Atto-specific options

toolbar
set to override which icons appear on the toolbar (normally it uses the admin setting - this is for special cases e.g. if you want a minimal editor in a particular plugin).

The following code will cause atto to show the four buttons indicated.

$attobuttons = 'style1 = bold, italic'. PHP_EOL .'list = unorderedlist, orderedlist';
$editor->use_editor($id, ['context' => $context, 'autosave' => false, 'atto:toolbar' => $attobuttons],
                ['return_types' => FILE_EXTERNAL]);