Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.
   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  namespace core\output;
  18  
  19  use moodle_exception;
  20  use renderable;
  21  use renderer_base;
  22  use templatable;
  23  
  24  /**
  25   * Renderable class for the comboboxsearch.
  26   *
  27   * @package    core_output
  28   * @copyright  2022 Mathew May <Mathew.solutions>
  29   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30   */
  31  class comboboxsearch implements renderable, templatable {
  32  
  33      /** @var bool $renderlater Should the dropdown render straightaway? */
  34      protected $renderlater;
  35  
  36      /** @var string $buttoncontent What is the content of the "Button" that users will always see. */
  37      protected $buttoncontent;
  38  
  39      /** @var null|string $dropdowncontent The content that can be passed in to render immediately. */
  40      protected $dropdowncontent;
  41  
  42      /** @var null|string $parentclasses Any special classes to put on the HTMLElement that contains the BS events. */
  43      protected $parentclasses;
  44  
  45      /** @var null|string $buttonclasses Any special classes to put on the HTMLElement that triggers the dropdown. */
  46      protected $buttonclasses;
  47  
  48      /** @var null|string $dropdownclasses Any special classes to put on the HTMLElement that contains the actual dropdown. */
  49      protected $dropdownclasses;
  50  
  51      /** @var null|string $buttonheader If the button item in the tertiary nav needs an extra top header for context. */
  52      protected $buttonheader;
  53  
  54      /** @var boolean $usesbutton Whether to provide a A11y button. */
  55      protected $usesbutton;
  56  
  57      /**
  58       * The class constructor.
  59       *
  60       * @param bool $renderlater How we figure out if we should render the template instantly.
  61       * @param string $buttoncontent What gets placed in the button.
  62       * @param ?string $dropdowncontent What can be placed in the dropdown if we are rendering now.
  63       * @param ?string $parentclasses The classes that can be added that the bootstrap events are attached to.
  64       * @param ?string $buttonclasses Any special classes that may be needed.
  65       * @param ?string $dropdownclasses Any special classes that may be needed.
  66       * @param ?string $buttonheader If the button item in the tertiary nav needs an extra top header for context.
  67       * @param bool $usebutton If we want the mustache to add the button roles for us or do we have another aria role node?
  68       * @throws moodle_exception If the implementor incorrectly call this module.
  69       */
  70      public function __construct(
  71          bool $renderlater,
  72          string $buttoncontent,
  73          ?string $dropdowncontent = null,
  74          ?string $parentclasses = null,
  75          ?string $buttonclasses = null,
  76          ?string $dropdownclasses = null,
  77          ?string $buttonheader = null,
  78          ?bool $usebutton = true
  79      ) {
  80          // Ensure implementors cant request to render the content now and not provide us any to show.
  81          if (!$renderlater && empty($dropdowncontent)) {
  82              throw new moodle_exception(
  83                  'incorrectdropdownvars',
  84                  'core',
  85                  '', null,
  86                  'Dropdown content must be set to render later.'
  87              );
  88          }
  89  
  90          $this->renderlater = $renderlater;
  91          $this->buttoncontent = $buttoncontent;
  92          $this->dropdowncontent = $dropdowncontent;
  93          $this->parentclasses = $parentclasses;
  94          $this->buttonclasses = $buttonclasses;
  95          $this->dropdownclasses = $dropdownclasses;
  96          $this->buttonheader = $buttonheader;
  97          $this->usesbutton = $usebutton;
  98      }
  99  
 100      /**
 101       * Export the data for the mustache template.
 102       *
 103       * @param renderer_base $output renderer to be used to render the action bar elements.
 104       * @return array
 105       */
 106      public function export_for_template(renderer_base $output): array {
 107          return [
 108              'rtl' => right_to_left(),
 109              'renderlater' => $this->renderlater,
 110              'buttoncontent' => $this->buttoncontent ,
 111              'dropdowncontent' => $this->dropdowncontent,
 112              'parentclasses' => $this->parentclasses,
 113              'buttonclasses' => $this->buttonclasses,
 114              'dropdownclasses' => $this->dropdownclasses,
 115              'buttonheader' => $this->buttonheader,
 116              'usebutton' => $this->usesbutton,
 117              'instance' => rand(), // Template uniqid is per render out so sometimes these conflict.
 118          ];
 119      }
 120  
 121      /**
 122       * Returns the standard template for the dropdown.
 123       *
 124       * @return string
 125       */
 126      public function get_template(): string {
 127          return 'core/comboboxsearch';
 128      }
 129  }