Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.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\navigation\output;
  18  
  19  use renderable;
  20  use renderer_base;
  21  use templatable;
  22  use custom_menu;
  23  
  24  /**
  25   * more menu navigation renderable
  26   *
  27   * @package     core
  28   * @category    navigation
  29   * @copyright   2021 onwards Adrian Greeve
  30   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31   */
  32  class more_menu implements renderable, templatable {
  33  
  34      protected $content;
  35      protected $navbarstyle;
  36      protected $haschildren;
  37      protected $istablist;
  38  
  39      /**
  40       * Constructor for this class.
  41       *
  42       * @param object $content Navigation objects.
  43       * @param string $navbarstyle class name.
  44       * @param bool $haschildren The content has children.
  45       * @param bool $istablist When true, the more menu should be rendered and behave with a tablist ARIA role.
  46       *                        If false, it's rendered with a menubar ARIA role. Defaults to false.
  47       */
  48      public function __construct(object $content, string $navbarstyle, bool $haschildren = true, bool $istablist = false) {
  49          $this->content = $content;
  50          $this->navbarstyle = $navbarstyle;
  51          $this->haschildren = $haschildren;
  52          $this->istablist = $istablist;
  53      }
  54  
  55      /**
  56       * Return data for rendering a template.
  57       *
  58       * @param renderer_base $output The output
  59       * @return array Data for rendering a template
  60       */
  61      public function export_for_template(renderer_base $output): array {
  62          $data = [
  63              'navbarstyle' => $this->navbarstyle,
  64              'istablist' => $this->istablist,
  65          ];
  66          if ($this->haschildren) {
  67              // The node collection doesn't have anything to render so exit now.
  68              if (!isset($this->content->children) || count($this->content->children) == 0) {
  69                  return [];
  70              }
  71              // Find all nodes that have children and are defined to show the children in a submenu.
  72              // For each of these nodes we would like to display a dropdown menu and in order to achieve that
  73              // (as required by the template) we need to set the node's property 'moremenuid' to a new unique value and
  74              // 'haschildren' to true.
  75              foreach ($this->content->children as &$item) {
  76                  if ($item->showchildreninsubmenu && isset($this->content->children) &&
  77                          count($this->content->children) > 0) {
  78                      $item->moremenuid = uniqid();
  79                      $item->haschildren = true;
  80                  }
  81              }
  82  
  83              $data['nodecollection'] = $this->content;
  84          } else {
  85              $data['nodearray'] = (array) $this->content;
  86          }
  87          $data['moremenuid'] = uniqid();
  88  
  89          return $data;
  90      }
  91  
  92  }