I'm having trouble adding a custom form element to my plugin. Essentially, it uses an existing element type, with custom HTML to be output before it. Here is a minimal example of my code:
local/my_plugin/classes/form/custom_text_input.php:
<?php namespace local_my_plugin\form; defined('MOODLE_INTERNAL') || die(); global $CFG; require_once($CFG->libdir . '/form/text.php'); class custom_text_input extends \MoodleQuickForm_text { /** * Constructor */ public function __construct($elementName=null, $elementLabel=null, $options=null, $attributes=null) { parent::__construct($elementName, $elementLabel, $options, $attributes); } /** * Returns HTML for the form element * * @return string */ function toHtml() { $html = 'Custom text'; $html .= parent::toHtml(); return $html; } }
local/my_plugin/classes/form.php:
<?php namespace local_my_plugin; use \html_writer; require_once("$CFG->libdir/formslib.php"); class form extends \moodleform { public function definition() { global $CFG; \MoodleQuickForm::registerElementType( 'custom_text_input', "$CFG->dirroot/local/my_plugin/classes/form/custom_text_input.php", 'local_my_plugin\form\custom_text_input' ); $mform = $this->_form; // Don't forget the underscore! $mform->addElement('custom_text_input', 'name', 'Test Element'); } }
What am I doing wrong? Why is only the input element showing up, and not the 'Custom text' which is applied before it in the toHtml() function?