Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 403]

   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.
  13  //
  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/>.
  16  
  17  /**
  18   * Adds export_for_template behaviour to an mform element in a consistent and predictable way.
  19   *
  20   * @package   core_form
  21   * @copyright 2016 Damyon Wiese
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  defined('MOODLE_INTERNAL') || die();
  25  
  26  // Some form elements are used before $CFG is created - do not rely on it here.
  27  require_once (__DIR__ . '/../outputcomponents.php');
  28  
  29  /**
  30   * templatable_form_element trait.
  31   *
  32   * @package   core_form
  33   * @copyright 2016 Damyon Wiese
  34   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  trait templatable_form_element {
  37  
  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).
  43       *
  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.
  46       *
  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
  49       */
  50      public function export_for_template(renderer_base $output) {
  51          $context = [];
  52  
  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'];
  56  
  57          // Standard attributes.
  58          foreach ($standardattributes as $attrname) {
  59              $value = $this->getAttribute($attrname);
  60              $context[$attrname] = $value;
  61          }
  62  
  63          // Standard class properties.
  64          foreach ($standardproperties as $propname) {
  65              $classpropname = '_' . $propname;
  66              $context[strtolower($propname)] = isset($this->$classpropname) ? $this->$classpropname : false;
  67          }
  68          $extraclasses = $this->getAttribute('class');
  69  
  70          // Special wierd named property.
  71          $context['frozen'] = !empty($this->_flagFrozen);
  72          $context['hardfrozen'] = !empty($this->_flagFrozen) && empty($this->_persistantFreeze);
  73  
  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) . '"';
  79              }
  80          }
  81          $context['extraclasses'] = $extraclasses;
  82          $context['type'] = $this->getType();
  83          $context['attributes'] = implode(' ', $otherattributes);
  84          $context['emptylabel'] = ($this->getLabel() === '');
  85          $context['iderror'] = preg_replace('/_id_/', '_id_error_', $context['id']);
  86          $context['iderror'] = preg_replace('/^id_/', 'id_error_', $context['iderror']);
  87  
  88          // Elements with multiple values need array syntax.
  89          if ($this->getAttribute('multiple')) {
  90              $context['name'] = $context['name'] . '[]';
  91          }
  92  
  93          return $context;
  94      }
  95  }