Commit 344321e1 authored by Damyon Wiese's avatar Damyon Wiese Committed by Dan Poltawski
Browse files

MDL-55417 forms: Clean up form element template patch

Use a trait and a base mustache template for all form elements to reduce code
duplication and increase standardisation.

Part of MDL-55071
parent 91bda4cd
......@@ -26,7 +26,7 @@
*/
require_once('HTML/QuickForm/advcheckbox.php');
require_once(__DIR__ . '/../outputcomponents.php');
require_once('templatable_form_element.php');
/**
* HTML class for an advcheckbox type element
......@@ -40,6 +40,9 @@ require_once(__DIR__ . '/../outputcomponents.php');
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class MoodleQuickForm_advcheckbox extends HTML_QuickForm_advcheckbox implements templatable {
use templatable_form_element;
/** @var string html for help button, if empty then no help will icon will be dispalyed. */
var $_helpbutton='';
......@@ -131,13 +134,4 @@ class MoodleQuickForm_advcheckbox extends HTML_QuickForm_advcheckbox implements
}
return $output;
}
public function export_for_template(renderer_base $output) {
$context = [];
$context['frozen'] = $this->_flagFrozen;
foreach ($this->getAttributes() as $name => $value) {
$context[$name] = $value;
}
return $context;
}
}
......@@ -40,6 +40,8 @@ require_once(__DIR__ . '/../outputcomponents.php');
*/
class MoodleQuickForm_button extends HTML_QuickForm_button implements templatable
{
use templatable_form_element;
/** @var string html for help button, if empty then no help */
var $_helpbutton='';
......@@ -86,13 +88,4 @@ class MoodleQuickForm_button extends HTML_QuickForm_button implements templatabl
return 'default';
}
}
public function export_for_template(renderer_base $output) {
$context = [];
$context['frozen'] = $this->_flagFrozen;
foreach ($this->getAttributes() as $name => $value) {
$context[$name] = $value;
}
return $context;
}
}
......@@ -26,7 +26,7 @@
*/
require_once('HTML/QuickForm/checkbox.php');
require_once(__DIR__ . '/../outputcomponents.php');
require_once('templatable_form_element.php');
/**
* HTML class for a checkbox type element
......@@ -41,6 +41,8 @@ require_once(__DIR__ . '/../outputcomponents.php');
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class MoodleQuickForm_checkbox extends HTML_QuickForm_checkbox implements templatable {
use templatable_form_element;
/** @var string html for help button, if empty then no help */
var $_helpbutton='';
......@@ -139,13 +141,4 @@ class MoodleQuickForm_checkbox extends HTML_QuickForm_checkbox implements templa
}
return $output;
}
public function export_for_template(renderer_base $output) {
$context = [];
$context['frozen'] = $this->_flagFrozen;
foreach ($this->getAttributes() as $name => $value) {
$context[$name] = $value;
}
return $context;
}
}
......@@ -30,7 +30,7 @@ global $CFG;
require_once('HTML/QuickForm/element.php');
require_once($CFG->dirroot.'/lib/filelib.php');
require_once($CFG->dirroot.'/repository/lib.php');
require_once($CFG->libdir.'/outputcomponents.php');
require_once('templatable_form_element.php');
/**
* Editor element
......@@ -45,6 +45,10 @@ require_once($CFG->libdir.'/outputcomponents.php');
* @todo MDL-29426 ajax format conversion
*/
class MoodleQuickForm_editor extends HTML_QuickForm_element implements templatable {
use templatable_form_element {
export_for_template as export_for_template_base;
}
/** @var string html for help button, if empty then no help will icon will be dispalyed. */
public $_helpbutton = '';
......@@ -451,11 +455,7 @@ class MoodleQuickForm_editor extends HTML_QuickForm_element implements templatab
}
public function export_for_template(renderer_base $output) {
$context = [];
$context['frozen'] = $this->_flagFrozen;
foreach ($this->getAttributes() as $name => $value) {
$context[$name] = $value;
}
$context = $this->export_for_template_base($output);
$context['html'] = $this->toHtml();
return $context;
}
......
......@@ -30,7 +30,7 @@ global $CFG;
require_once('HTML/QuickForm/element.php');
require_once($CFG->dirroot.'/lib/filelib.php');
require_once($CFG->dirroot.'/repository/lib.php');
require_once($CFG->libdir.'/outputcomponents.php');
require_once('templatable_form_element.php');
/**
* Filemanager form element
......@@ -42,6 +42,10 @@ require_once($CFG->libdir.'/outputcomponents.php');
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class MoodleQuickForm_filemanager extends HTML_QuickForm_element implements templatable {
use templatable_form_element {
export_for_template as export_for_template_base;
}
/** @var string html for help button, if empty then no help will icon will be dispalyed. */
public $_helpbutton = '';
......@@ -300,11 +304,7 @@ class MoodleQuickForm_filemanager extends HTML_QuickForm_element implements temp
}
public function export_for_template(renderer_base $output) {
$context = [];
$context['frozen'] = $this->_flagFrozen;
foreach ($this->getAttributes() as $name => $value) {
$context[$name] = $value;
}
$context = $this->export_for_template_base($output);
$context['html'] = $this->toHtml();
return $context;
}
......
......@@ -29,7 +29,7 @@ global $CFG;
require_once("HTML/QuickForm/button.php");
require_once($CFG->dirroot.'/repository/lib.php');
require_once($CFG->libdir.'/outputcomponents.php');
require_once('templatable_form_element.php');
/**
* Filepicker form element
......@@ -42,6 +42,9 @@ require_once($CFG->libdir.'/outputcomponents.php');
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class MoodleQuickForm_filepicker extends HTML_QuickForm_input implements templatable {
use templatable_form_element {
export_for_template as export_for_template_base;
}
/** @var string html for help button, if empty then no help will icon will be dispalyed. */
public $_helpbutton = '';
......@@ -223,11 +226,7 @@ class MoodleQuickForm_filepicker extends HTML_QuickForm_input implements templat
}
public function export_for_template(renderer_base $output) {
$context = [];
$context['frozen'] = $this->_flagFrozen;
foreach ($this->getAttributes() as $name => $value) {
$context[$name] = $value;
}
$context = $this->export_for_template_base($output);
$context['html'] = $this->toHtml();
return $context;
}
......
......@@ -28,7 +28,7 @@
global $CFG;
require_once("HTML/QuickForm/element.php");
require_once($CFG->dirroot.'/grade/grading/form/lib.php');
require_once($CFG->libdir.'/outputcomponents.php');
require_once('templatable_form_element.php');
if (class_exists('HTML_QuickForm')) {
HTML_QuickForm::registerRule('gradingvalidated', 'callback', '_validate', 'MoodleQuickForm_grading');
......@@ -49,6 +49,10 @@ if (class_exists('HTML_QuickForm')) {
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class MoodleQuickForm_grading extends HTML_QuickForm_input implements templatable {
use templatable_form_element {
export_for_template as export_for_template_base;
}
/** @var string html for help button, if empty then no help */
var $_helpbutton='';
......@@ -162,6 +166,8 @@ class MoodleQuickForm_grading extends HTML_QuickForm_input implements templatabl
}
public function export_for_template(renderer_base $output) {
return $this->toHtml();
$context = $this->export_for_template_base($output);
$context['html'] = $this->toHtml();
return $context;
}
}
......@@ -26,7 +26,7 @@
*/
require_once("HTML/QuickForm/group.php");
require_once(__DIR__ . '/../outputcomponents.php');
require_once('templatable_form_element.php');
/**
* HTML class for a form element group
......@@ -39,6 +39,10 @@ require_once(__DIR__ . '/../outputcomponents.php');
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class MoodleQuickForm_group extends HTML_QuickForm_group implements templatable {
use templatable_form_element {
export_for_template as export_for_template_base;
}
/** @var string html for help button, if empty then no help */
var $_helpbutton='';
......@@ -153,15 +157,13 @@ class MoodleQuickForm_group extends HTML_QuickForm_group implements templatable
public function export_for_template(renderer_base $output) {
global $OUTPUT;
$context = $this->export_for_template_base($output);
$this->_renderedfromtemplate = true;
include_once('HTML/QuickForm/Renderer/Default.php');
$context = [];
$context['frozen'] = $this->_flagFrozen;
foreach ($this->getAttributes() as $name => $value) {
$context[$name] = $value;
}
$elements = [];
foreach ($this->_elements as $key => $element) {
$element->_generateId();
......
......@@ -26,7 +26,7 @@
*/
require_once('HTML/QuickForm/password.php');
require_once(__DIR__ . '/../outputcomponents.php');
require_once('templatable_form_element.php');
/**
* Password type form element
......@@ -39,6 +39,8 @@ require_once(__DIR__ . '/../outputcomponents.php');
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class MoodleQuickForm_password extends HTML_QuickForm_password implements templatable {
use templatable_form_element;
/** @var string, html for help button, if empty then no help */
var $_helpbutton='';
......@@ -84,13 +86,4 @@ class MoodleQuickForm_password extends HTML_QuickForm_password implements templa
function getHelpButton(){
return $this->_helpbutton;
}
public function export_for_template(renderer_base $output) {
$context = [];
$context['frozen'] = $this->_flagFrozen;
foreach ($this->getAttributes() as $name => $value) {
$context[$name] = $value;
}
return $context;
}
}
......@@ -26,7 +26,7 @@
*/
require_once('HTML/QuickForm/radio.php');
require_once('templatable_form_element.php');
/**
* radio type form element
*
......@@ -37,7 +37,9 @@ require_once('HTML/QuickForm/radio.php');
* @copyright 2006 Jamie Pratt <me@jamiep.org>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class MoodleQuickForm_radio extends HTML_QuickForm_radio{
class MoodleQuickForm_radio extends HTML_QuickForm_radio implements templatable {
use templatable_form_element;
/** @var string html for help button, if empty then no help */
var $_helpbutton='';
......
......@@ -26,7 +26,7 @@
*/
require_once('HTML/QuickForm/input.php');
require_once(__DIR__ . '/../outputcomponents.php');
require_once('templatable_form_element.php');
/**
* recaptcha type form element
......@@ -39,6 +39,9 @@ require_once(__DIR__ . '/../outputcomponents.php');
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class MoodleQuickForm_recaptcha extends HTML_QuickForm_input implements templatable {
use templatable_form_element {
export_for_template as export_for_template_base;
}
/** @var string html for help button, if empty then no help */
var $_helpbutton='';
......@@ -155,11 +158,7 @@ class MoodleQuickForm_recaptcha extends HTML_QuickForm_input implements templata
}
public function export_for_template(renderer_base $output) {
$context = [];
$context['frozen'] = $this->_flagFrozen;
foreach ($this->getAttributes() as $name => $value) {
$context[$name] = $value;
}
$context = $this->export_for_template_base($output);
$context['html'] = $this->toHtml();
return $context;
}
......
......@@ -26,7 +26,7 @@
*/
require_once('HTML/QuickForm/select.php');
require_once(__DIR__ . '/../outputcomponents.php');
require_once('templatable_form_element.php');
/**
* select type form element
......@@ -39,6 +39,11 @@ require_once(__DIR__ . '/../outputcomponents.php');
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class MoodleQuickForm_select extends HTML_QuickForm_select implements templatable {
use templatable_form_element {
export_for_template as export_for_template_base;
}
/** @var string html for help button, if empty then no help */
var $_helpbutton='';
......@@ -192,15 +197,7 @@ class MoodleQuickForm_select extends HTML_QuickForm_select implements templatabl
}
public function export_for_template(renderer_base $output) {
$context = [];
$context['frozen'] = $this->_flagFrozen;
$context['attributes'] = [];
foreach ($this->getAttributes() as $name => $value) {
$context[$name] = $value;
if (!in_array($name, ['id', 'name', 'multiple'])) {
$context['attributes'][] = ['name' => $name, 'value' => $value];
}
}
$context = $this->export_for_template_base($output);
$options = [];
foreach ($this->_options as $option) {
......@@ -215,7 +212,6 @@ class MoodleQuickForm_select extends HTML_QuickForm_select implements templatabl
$options[] = $o;
}
$context['options'] = $options;
$context['hideLabel'] = $this->_hiddenLabel;
return $context;
}
......
......@@ -26,7 +26,7 @@
*/
require_once('HTML/QuickForm/element.php');
require_once(__DIR__ . '/../outputcomponents.php');
require_once('templatable_form_element.php');
/**
* select type form element
......@@ -40,6 +40,10 @@ require_once(__DIR__ . '/../outputcomponents.php');
*/
class MoodleQuickForm_selectgroups extends HTML_QuickForm_element implements templatable {
use templatable_form_element {
export_for_template as export_for_template_base;
}
/** @var bool add choose option */
var $showchoose = false;
......@@ -510,15 +514,7 @@ class MoodleQuickForm_selectgroups extends HTML_QuickForm_element implements tem
}
public function export_for_template(renderer_base $output) {
$context = [];
$context['frozen'] = $this->_flagFrozen;
$context['attributes'] = [];
foreach ($this->getAttributes() as $name => $value) {
$context[$name] = $value;
if (!in_array($name, ['id', 'name', 'multiple'])) {
$context['attributes'][] = ['name' => $name, 'value' => $value];
}
}
$context = $this->export_for_template_base($output);
$optiongroups = [];
if ($this->showchoose) {
$optionsgroups[] = [
......@@ -548,7 +544,6 @@ class MoodleQuickForm_selectgroups extends HTML_QuickForm_element implements tem
$optiongroups[] = $og;
}
$context['optiongroups'] = $optiongroups;
$context['hideLabel'] = $this->_hiddenLabel;
return $context;
}
......
......@@ -26,7 +26,7 @@
*/
require_once("HTML/QuickForm/static.php");
require_once(__DIR__ . '/../outputcomponents.php');
require_once('templatable_form_element.php');
/**
* Text type element
......@@ -39,6 +39,10 @@ require_once(__DIR__ . '/../outputcomponents.php');
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class MoodleQuickForm_static extends HTML_QuickForm_static implements templatable {
use templatable_form_element {
export_for_template as export_for_template_base;
}
/** @var string Form element type */
var $_elementTemplateType='static';
......@@ -85,11 +89,7 @@ class MoodleQuickForm_static extends HTML_QuickForm_static implements templatabl
}
public function export_for_template(renderer_base $output) {
$context = [];
$context['frozen'] = $this->_flagFrozen;
foreach ($this->getAttributes() as $name => $value) {
$context[$name] = $value;
}
$context = $this->export_for_template_base($output);
$context['html'] = $this->toHtml();
return $context;
}
......
......@@ -26,7 +26,7 @@
*/
require_once("HTML/QuickForm/submit.php");
require_once(__DIR__ . '/../outputcomponents.php');
require_once('templatable_form_element.php');
/**
* submit type form element
......@@ -39,6 +39,10 @@ require_once(__DIR__ . '/../outputcomponents.php');
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class MoodleQuickForm_submit extends HTML_QuickForm_submit implements templatable {
use templatable_form_element {
export_for_template as export_for_template_base;
}
/**
* constructor
*
......@@ -110,11 +114,7 @@ class MoodleQuickForm_submit extends HTML_QuickForm_submit implements templatabl
}
public function export_for_template(renderer_base $output) {
$context = [];
$context['frozen'] = $this->_flagFrozen;
foreach ($this->getAttributes() as $name => $value) {
$context[$name] = $value;
}
$context = $this->export_for_template_base($output);
if ($this->getName() == 'cancel') {
$context['iscancel'] = true;
}
......
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Adds export_for_template behaviour to an mform element in a consistent and predictable way.
*
* @package core_form
* @copyright 2016 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
// Some form elements are used before $CFG is created - do not rely on it here.
require_once(__DIR__ . '/../outputcomponents.php');
/**
* templatable_form_element trait.
*
* @package core_form
* @copyright 2016 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
trait templatable_form_element {
/**
* Function to export the renderer data in a format that is suitable for a
* mustache template. This means:
* 1. No complex types - only stdClass, array, int, string, float, bool
* 2. Any additional info that is required for the template is pre-calculated (e.g. capability checks).
*
* This trait can be used as-is for simple form elements - or imported with a different name
* so it can be extended with additional context variables before being returned.
*
* @param renderer_base $output Used to do a final render of any components that need to be rendered for export.
* @return stdClass|array
*/
public function export_for_template(renderer_base $output) {
$context = [];
// Not all elements have all of these attributes - but they are common enough to be valid for a few.
$standardattributes = ['id', 'name', 'label', 'multiple', 'checked', 'error', 'size', 'value'];
$standardproperties = ['frozen', 'helpbutton', 'hiddenLabel'];
// Standard attributes.
foreach ($standardattributes as $attrname) {
$value = $this->getAttribute($attrname);
$context[$attrname] = $value;
}
// Standard class properties.
foreach ($standardproperties as $propname) {
$classpropname = '_' . $propname;
$context[strtolower($propname)] = isset($this->$classpropname) ? $this->$classpropname : false;
}
// Other attributes.
$otherattributes = [];
foreach ($this->getAttributes() as $attr => $value) {
if (!in_array($attr, $standardattributes)) {
$otherattributes[] = $attr . '="' . s($value) . '"';
}
}
$context['attributes'] = implode(' ', $otherattributes);
return $context;
}
}
\ No newline at end of file
......@@ -26,7 +26,7 @@
*/
require_once("HTML/QuickForm/text.php");
require_once(__DIR__ . '/../outputcomponents.php');
require_once('templatable_form_element.php');
/**
* Text type form element
......@@ -39,6 +39,7 @@ require_once(__DIR__ . '/../outputcomponents.php');
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class MoodleQuickForm_text extends HTML_QuickForm_text implements templatable {
use templatable_form_element;
/** @var string html for help button, if empty then no help */
var $_helpbutton='';
......@@ -127,13 +128,4 @@ class MoodleQuickForm_text extends HTML_QuickForm_text implements templatable {
return $this->_helpbutton;
}
public function export_for_template(renderer_base $output) {
$context = [];
$context['frozen'] = $this->_flagFrozen;
foreach ($this->getAttributes() as $name => $value) {
$context[$name] = $value;
}
$context['hideLabel'] = $this->_hiddenLabel;
return $context;
}
}
......@@ -26,7 +26,7 @@
*/
require_once('HTML/QuickForm/textarea.php');
require_once(__DIR__ . '/../outputcomponents.php');
require_once('templatable_form_element.php');
/**
* Textarea type form element
......@@ -39,6 +39,8 @@ require_once(__DIR__ . '/../outputcomponents.php');
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class MoodleQuickForm_textarea extends HTML_QuickForm_textarea implements templatable {
use templatable_form_element;
/** @var string Need to store id of form as we may need it for helpbutton */
var $_formid = '';
......@@ -131,14 +133,4 @@ class MoodleQuickForm_textarea extends HTML_QuickForm_textarea implements templa
return 'default';
}
}
public function export_for_template(renderer_base $output) {
$context = [];
$context['frozen'] = $this->_flagFrozen;
foreach ($this->getAttributes() as $name => $value) {
$context[$name] = $value;
}
$context['hideLabel'] = $this->_hiddenLabel;
return $context;
}
}