MDL-55417 forms: Render form elements with a template
This change allows form elements to be overridden with a mustache template. The template can even listen for form validation errors and supply the JS to change the look of the form element when there is/isn't a validation error. Initial support is for all core form elements including: text, select, selectyesno and checkboxes, groups, dateselector, datetimeselector, autocomplete, modvisible, advcheckbox, button, duration, filemanager, filepicker, editor, static, grading, warning, textarea, password, url, submit, questioncategory, recaptcha. Part of MDL-55071
This commit is contained in:
committed by
Dan Poltawski
parent
ef18a21f93
commit
91bda4cd45
@@ -4421,6 +4421,51 @@ EOD;
|
||||
return $this->render_from_template('core/login', $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders an mform element from a template.
|
||||
*
|
||||
* @param HTML_QuickForm_element $element element
|
||||
* @param bool $required if input is required field
|
||||
* @param string $error error message to display
|
||||
* @param bool $ingroup True if this element is rendered as part of a group
|
||||
* @return mixed string|bool
|
||||
*/
|
||||
public function mform_element($element, $required, $error, $ingroup) {
|
||||
$templatename = 'core_form/element-' . $element->getType();
|
||||
if ($ingroup) {
|
||||
$templatename .= "-inline";
|
||||
}
|
||||
try {
|
||||
// We call this to generate a file not found exception if there is no template.
|
||||
// We don't want to call export_for_template if there is no template.
|
||||
core\output\mustache_template_finder::get_template_filepath($templatename);
|
||||
|
||||
if ($element instanceof templatable) {
|
||||
$elementcontext = $element->export_for_template($this);
|
||||
|
||||
$helpbutton = '';
|
||||
if (method_exists($element, 'getHelpButton')) {
|
||||
$helpbutton = $element->getHelpButton();
|
||||
}
|
||||
$label = $element->getLabel();
|
||||
if (method_exists($element, 'getText')) {
|
||||
$label .= ' ' . $element->getText();
|
||||
}
|
||||
|
||||
$context = array(
|
||||
'element' => $elementcontext,
|
||||
'label' => $label,
|
||||
'required' => $required,
|
||||
'helpbutton' => $helpbutton,
|
||||
'error' => $error
|
||||
);
|
||||
return $this->render_from_template($templatename, $context);
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
// No template for this element.
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user