Merge branch 'MDL-64012' of https://github.com/timhunt/moodle
[moodle.git] / lib / form / templatable_form_element.php
blobe0b235a22e82496cc2846d5209c55af8189fba50
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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/>.
17 /**
18 * Adds export_for_template behaviour to an mform element in a consistent and predictable way.
20 * @package core_form
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');
29 /**
30 * templatable_form_element trait.
32 * @package core_form
33 * @copyright 2016 Damyon Wiese
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 trait templatable_form_element {
38 /**
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) {
51 $context = [];
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');
70 // Special wierd named property.
71 $context['frozen'] = !empty($this->_flagFrozen);
72 $context['hardfrozen'] = !empty($this->_flagFrozen) && empty($this->_persistantFreeze);
74 // Other attributes.
75 $otherattributes = [];
76 foreach ($this->getAttributes() as $attr => $value) {
77 if (!in_array($attr, $standardattributes) && $attr != 'class' && !is_object($value)) {
78 $otherattributes[] = $attr . '="' . s($value) . '"';
81 $context['extraclasses'] = $extraclasses;
82 $context['type'] = $this->getType();
83 $context['attributes'] = implode(' ', $otherattributes);
84 $context['emptylabel'] = ($this->getLabel() === '');
86 // Elements with multiple values need array syntax.
87 if ($this->getAttribute('multiple')) {
88 $context['name'] = $context['name'] . '[]';
91 return $context;