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\local\action_menu;
  18  
  19  use action_link;
  20  use pix_icon;
  21  use renderable;
  22  use stdClass;
  23  
  24  /**
  25   * Interface to a subpanel implementation.
  26   *
  27   * @package    core_admin
  28   * @copyright  2023 Ferran Recio <ferran@moodle.com>
  29   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30   */
  31  class subpanel extends action_link implements renderable {
  32      /**
  33       * The subpanel content.
  34       * @var renderable
  35       */
  36      protected $subpanel;
  37  
  38      /**
  39       * The number of instances of this action menu link (and its subclasses).
  40       * @var int
  41       */
  42      protected static $instance = 1;
  43  
  44      /**
  45       * Constructor.
  46       * @param string $text the text to display
  47       * @param renderable $subpanel the subpanel content
  48       * @param array|null $attributes an optional array of attributes
  49       * @param pix_icon|null $icon an optional icon
  50       */
  51      public function __construct(
  52          $text,
  53          renderable $subpanel,
  54          array $attributes = null,
  55          pix_icon $icon = null
  56      ) {
  57          $this->text = $text;
  58          $this->subpanel = $subpanel;
  59          if (empty($attributes['id'])) {
  60              $attributes['id'] = \html_writer::random_id('action_menu_submenu');
  61          }
  62          $this->attributes = (array) $attributes;
  63          $this->icon = $icon;
  64      }
  65  
  66      /**
  67       * Export this object for template rendering.
  68       * @param \renderer_base $output the output renderer
  69       * @return stdClass
  70       */
  71      public function export_for_template(\renderer_base $output): stdClass {
  72          $data = parent::export_for_template($output);
  73          $data->instance = self::$instance++;
  74          $data->subpanelcontent = $output->render($this->subpanel);
  75          // The menu trigger icon collides with the subpanel item icon. Unlike regular menu items,
  76          // subpanel items usually does not use icons. To prevent the collision, subpanels use a diferent
  77          // context variable for item icon.
  78          $data->itemicon = $data->icon;
  79          unset($data->icon);
  80          return $data;
  81      }
  82  }