An editor module implements a web interface for editing the content of questions, answers and comments. The module controls the HTML field which is displayed for editing, and converts the input from that field into data for storage in Q2A’s database.
For example, the default editor module in
qa-editor-basic.php displays a simple text field and generates text content for storage. The WYSIWYG Editor plugin included with Q2A wraps CKEditor and generates text or HTML content for storage. Your editor can also store content in a format other than text or HTML, but a viewer module will be required to render that content for Q2A.
class for an editor module must contain the following functions (all are required):
calc_quality($content, $format)should return a numerical value indicating your editor’s compatibility with the supplied content, as retrieved from Q2A’s database. If
$contentcontains plain text in UTF-8 encoding. If
$contentcontains HTML with UTF-8 encoding. Other values of
$formatare also possible, depending on the editor modules installed. You should return
1.0to indicate perfect compatibility, and
0.0for complete incompatibility. If an editor has been selected as the default in Q2A’s admin panel, it will be used if its
0.5or more for the given content. Otherwise, the editor returning the highest value will be used. For your reference, Q2A’s basic text editor returns
1.0for plain text and
0.2for HTML, and the WYSIWYG Editor plugin returns
1.0for HTML and
0.8for plain text.
get_field(&$qa_content, $content, $format, $fieldname, $rows)should return an HTML-based field for your editor. The
$formatparameters specify the content that needs editing or
nullif new content is being created. The
$fieldnameparameter contains the HTML element name that you should use - if your editor outputs multiple HTML elements, use
$fieldnameas a prefix. The
$rowsparameter indicates a suggested height for your editor, in lines of text. To output custom HTML for your editor, return
$qa_contentarray - see
qa-wysiwyg-editor.phpfor some examples, or ask here.
read_post($fieldname)should retrieve the content from your editor, as POSTed from the user’s web browser, and convert it for storage in Q2A’s database. The
$fieldnameparameter matches the value that was previously passed to
get_field(). To store plain text, return
array('format' => '', 'content' => '[text in UTF-8]')from this function. To store HTML, return
array('format' => 'html', 'content' => '[html in UTF-8]'). If you are storing HTML as submitted by the user’s browser, you must sanitize it using
read_post()function can store any other type of content in Q2A’s database, by supplying an appropriate
'format'(up to 20 ASCII characters in length) and corresponding
'content'. In this case, a viewer module will be required to render the stored content as plain text or HTML.
In addition, the following optional functions may also be defined (all require Q2A 1.5+):
"document.getElementById('".$fieldname."').focus();". Note that this function replaces the deprecated
$autofocusparameter passed to
get_field()in earlier versions of Q2A.