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  /**
  18   * Moodle Component Library
  19   *
  20   * A sample of a default action menu.
  21   *
  22   * @package    tool_componentlibrary
  23   * @copyright  2023 Ferran Recio <ferran@moodle.com>
  24   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  
  27  declare(strict_types=1);
  28  
  29  require_once(__DIR__ . '/../../../../config.php');
  30  
  31  global $PAGE;
  32  
  33  $PAGE->set_url(new moodle_url('/admin/tool/componentlibrary/examples/actionmenu.php'));
  34  $PAGE->set_context(context_system::instance());
  35  $PAGE->set_pagelayout('embedded');
  36  
  37  $PAGE->set_heading('Moodle action menus');
  38  $PAGE->set_title('Moodle action menus');
  39  
  40  /** @var core_renderer $output*/
  41  $output = $PAGE->get_renderer('core');
  42  
  43  // Some menu items require as renderable element. This is just an
  44  // example of a choice list but it can be any other renderable.
  45  $choice = new core\output\choicelist('Choice example');
  46  $choice->add_option("statusa", "Status A", [
  47      'url' => $PAGE->url,
  48      'description' => 'Status A description',
  49      'icon' => new pix_icon('t/user', '', ''),
  50  ]);
  51  $choice->add_option("statusb", "Status B", [
  52      'url' => $PAGE->url,
  53      'description' => 'Status B description',
  54      'icon' => new pix_icon('t/groupv', '', ''),
  55  ]);
  56  $choice->set_selected_value('statusb');
  57  
  58  // Those are some examples of action items.
  59  
  60  // Action menu links is the most used action item.
  61  $basicactionlink = new action_menu_link(
  62      new moodle_url($PAGE->url),
  63      new pix_icon('t/emptystar', ''),
  64      'Action link example',
  65      false
  66  );
  67  
  68  // Subpanels display lateral panels on hovered or clicked.
  69  $subpanel = new core\output\local\action_menu\subpanel(
  70      'Subpanel example',
  71      $choice
  72  );
  73  
  74  echo $output->header();
  75  
  76  echo '<p><strong>Important note:</strong> actions menus are not prepared
  77      to be displayed inside iframes. You may need to scroll to see the
  78      action menu options.</p>';
  79  
  80  echo $output->heading("Action menu default example", 4);
  81  
  82  $menu = new action_menu();
  83  
  84  $menu->add($basicactionlink);
  85  $menu->add($basicactionlink);
  86  $menu->add($subpanel);
  87  $menu->add($basicactionlink);
  88  
  89  echo '<div class="border m-3 p-3 d-flex flex-row">';
  90  echo '<div class="flex-fill">Example of default an action menu</div><div>';
  91  echo $OUTPUT->render($menu);
  92  echo '</div></div>';
  93  
  94  echo $output->heading("Kebab menu example", 4);
  95  
  96  $menu = new action_menu();
  97  $menu->set_kebab_trigger(get_string('edit'), $output);
  98  $menu->set_additional_classes('fields-actions');
  99  
 100  $menu->add($basicactionlink);
 101  $menu->add($basicactionlink);
 102  $menu->add(new core\output\local\action_menu\subpanel(
 103      'Subpanel example',
 104      $choice
 105  ));
 106  $menu->add($basicactionlink);
 107  
 108  echo '<div class="border m-3 p-3 d-flex flex-row">';
 109  echo '<div class="flex-fill">Example of kebab menu</div><div>';
 110  echo $OUTPUT->render($menu);
 111  echo '</div></div>';
 112  
 113  echo $output->heading("Custom trigger menu example", 4);
 114  
 115  $menu = new action_menu();
 116  $menu->set_menu_trigger(get_string('edit'));
 117  
 118  $menu->add($basicactionlink);
 119  $menu->add($basicactionlink);
 120  $menu->add(new core\output\local\action_menu\subpanel(
 121      'Subpanel example',
 122      $choice
 123  ));
 124  $menu->add($basicactionlink);
 125  
 126  echo '<div class="border m-3 p-3 d-flex flex-row">';
 127  echo '<div class="flex-fill">Example of kebab menu</div><div>';
 128  echo $OUTPUT->render($menu);
 129  echo '</div></div>';
 130  
 131  echo $output->heading("Primary actions menu example", 4);
 132  
 133  $menu = new action_menu();
 134  $menu->set_menu_trigger(get_string('edit'));
 135  
 136  $menu->add($basicactionlink);
 137  $menu->add($basicactionlink);
 138  $menu->add(new core\output\local\action_menu\subpanel(
 139      'Subpanel example',
 140      $choice
 141  ));
 142  $menu->add($basicactionlink);
 143  $menu->add(new action_menu_link_primary(
 144      $PAGE->url,
 145      new pix_icon('t/emptystar', ''),
 146      'Action link example',
 147  ));
 148  $menu->add(new action_menu_link_primary(
 149      $PAGE->url,
 150      new pix_icon('t/user', ''),
 151      'Action link example',
 152  ));
 153  
 154  echo '<div class="border m-3 p-3 d-flex flex-row">';
 155  echo '<div class="flex-fill">Example of a menu with primary actions</div><div>';
 156  echo $OUTPUT->render($menu);
 157  echo '</div></div>';
 158  
 159  echo $output->footer();