2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Adds export_for_template behaviour to an mform element in a consistent and predictable way.
21 * @copyright 2016 Damyon Wiese
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 defined('MOODLE_INTERNAL') ||
die();
26 // Some form elements are used before $CFG is created - do not rely on it here.
27 require_once(__DIR__
. '/../outputcomponents.php');
30 * templatable_form_element trait.
33 * @copyright 2016 Damyon Wiese
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 trait templatable_form_element
{
39 * Function to export the renderer data in a format that is suitable for a
40 * mustache template. This means:
41 * 1. No complex types - only stdClass, array, int, string, float, bool
42 * 2. Any additional info that is required for the template is pre-calculated (e.g. capability checks).
44 * This trait can be used as-is for simple form elements - or imported with a different name
45 * so it can be extended with additional context variables before being returned.
47 * @param renderer_base $output Used to do a final render of any components that need to be rendered for export.
48 * @return stdClass|array
50 public function export_for_template(renderer_base
$output) {
53 // Not all elements have all of these attributes - but they are common enough to be valid for a few.
54 $standardattributes = ['id', 'name', 'label', 'multiple', 'checked', 'error', 'size', 'value', 'type'];
55 $standardproperties = ['helpbutton', 'hiddenLabel'];
57 // Standard attributes.
58 foreach ($standardattributes as $attrname) {
59 $value = $this->getAttribute($attrname);
60 $context[$attrname] = $value;
63 // Standard class properties.
64 foreach ($standardproperties as $propname) {
65 $classpropname = '_' . $propname;
66 $context[strtolower($propname)] = isset($this->$classpropname) ?
$this->$classpropname : false;
68 $extraclasses = $this->getAttribute('class');
69 $parentonlyclasses = $this->getAttribute('parentclass');
71 // Special wierd named property.
72 $context['frozen'] = !empty($this->_flagFrozen
);
73 $context['hardfrozen'] = !empty($this->_flagFrozen
) && empty($this->_persistantFreeze
);
76 $otherattributes = [];
77 foreach ($this->getAttributes() as $attr => $value) {
78 if (!in_array($attr, $standardattributes) && $attr != 'class' && $attr != 'parentclass' && !is_object($value)) {
79 $otherattributes[] = $attr . '="' . s($value) . '"';
82 $context['extraclasses'] = $extraclasses;
83 $context['parentclasses'] = $parentonlyclasses;
84 $context['type'] = $this->getType();
85 $context['attributes'] = implode(' ', $otherattributes);
86 $context['emptylabel'] = ($this->getLabel() === '');
87 $context['iderror'] = preg_replace('/_id_/', '_id_error_', $context['id']);
88 $context['iderror'] = preg_replace('/^id_/', 'id_error_', $context['iderror']);
90 // Elements with multiple values need array syntax.
91 if ($this->getAttribute('multiple')) {
92 $context['name'] = $context['name'] . '[]';