Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
---
2
layout: docs
3
title: "Action menus"
4
description: "A reusable action menu component"
5
date: 2023-07-27T10:10:00+08:00
6
draft: false
7
tags:
8
- MDL-78665
9
- "4.3"
10
---
11
 
12
## How it works
13
 
14
Moodle action menus are a reusable component that can display a list of actions in a dropdown menu. They are used in many places in Moodle, including the user menu, the course administration menu, and the activity administration menu.
15
 
16
## Source files
17
 
18
- `lib/outputcomponents.php`: contains the main `action_menu`, `action_menu_link` and `pix_icon`.
19
- `lib/classes/output/local/action_menu/subpanel.php`: contains the `subpanel` menu item class.
20
- `lib/templates/action_menu.mustache`: contains the main template for the action menu.
21
- `lib/templates/action_menu_*`: location for the legacy auxliar mustache files.
22
- `lib/templates/local/action_menu/*`: location for any new auxiliar mustache files.
23
 
24
## Examples
25
 
26
<!-- markdownlint-disable-next-line MD033 -->
27
<iframe src="../../../../examples/actionmenu.php" style="overflow:hidden;height:400px;width:100%;border:0" title="Moodle action menus"></iframe>
28
 
29
## Usage
30
 
31
### Rendering an action menu
32
 
33
The component output classes can render an action menu entirely in PHP. The steps to do it are:
34
 
35
1. Create an action menu instance (with or without items)
36
2. (optional) Setup the action menu trigger
37
3. (optional) Add items to the menu (if they are not added on creation)
38
4. Render the menu.
39
 
40
The following code is a basic example of an action menu:
41
 
42
{{< php >}}
43
/** @var core_renderer $output*/
44
$output = $PAGE->get_renderer('core');
45
 
46
$menu = new action_menu();
47
 
48
// Add items.
49
$menu->add(new action_menu_link(
50
    new moodle_url($PAGE->url, ['foo' => 'bar']),
51
    new pix_icon('t/emptystar', ''),
52
    'Action link example',
53
    false
54
));
55
 
56
echo $output->render($menu);
57
{{< / php >}}
58
 
59
And this is the same example but passing the items in the creation:
60
 
61
{{< php >}}
62
/** @var core_renderer $output*/
63
$output = $PAGE->get_renderer('core');
64
 
65
$menu = new action_menu([
66
    new action_menu_link(
67
        new moodle_url($PAGE->url, ['foo' => 'bar']),
68
        new pix_icon('t/emptystar', ''),
69
        'Action link example',
70
        false
71
    ),
72
]);
73
 
74
echo $output->render($menu);
75
{{< / php >}}
76
 
77
### Setup the menu trigger
78
 
79
By default, the action menu trigger is a cog icon. However, the class has methods to convert it to a kebab menu or even display any arbitrary content.
80
 
81
Example of a kebab menu:
82
 
83
{{< php >}}
84
/** @var core_renderer $output*/
85
$output = $PAGE->get_renderer('core');
86
 
87
$menu = new action_menu();
88
$menu->set_kebab_trigger(get_string('edit'), $output);
89
$menu->set_additional_classes('fields-actions');
90
{{< / php >}}
91
 
92
Example of a custom trigger:
93
 
94
{{< php >}}
95
/** @var core_renderer $output*/
96
$output = $PAGE->get_renderer('core');
97
 
98
$menu = new action_menu();
99
$menu->set_menu_trigger(get_string('edit'));
100
{{< / php >}}
101
 
102
### Add items
103
 
104
Items can be added as an array on creation or using the `add` method. Depending on the param passed to `add` the item can be displayed in two different locations:
105
 
106
Primary items: are displayed next to the trigger button as direct actions.
107
Secondary items: are displayed inside the action menu dropdown.
108
 
109
The item location must be configured before adding the element. The following example shows different ways to add primary and secondary menu items.
110
 
111
{{< php >}}
112
// Primary items examples.
113
$menu->add(new action_menu_link(
114
    new moodle_url($PAGE->url),
115
    new pix_icon('t/emptystar', ''),
116
    'Action link example',
117
    true
118
));
119
$menu->add(new action_menu_link_primary(
120
    $PAGE->url,
121
    new pix_icon('t/emptystar', ''),
122
    'Action link example',
123
));
124
 
125
// Secondary items examples.
126
$menu->add(new action_menu_link(
127
    new moodle_url($PAGE->url),
128
    new pix_icon('t/emptystar', ''),
129
    'Action link example',
130
    false
131
));
132
$menu->add(new action_menu_link_secondary(
133
    $PAGE->url,
134
    new pix_icon('t/user', ''),
135
    'Action link example',
136
));
137
{{< / php >}}
138
 
139
## Types of items
140
 
141
The `add` method accepts several item types.
142
 
143
### `action_menu_link`
144
 
145
The `action_menu_link` class is the generic class for link items. It has several construct params:
146
 
147
- `moodle_url $url`: the link URL.
148
- `pix_icon $icon`: an optional pix_icon. If none passed, the item will show the trigger icon or none if it is a kebab menu.
149
- `string $text`: the text to display.
150
- `bool $primary`: if the item is primary or secondary. By default, all items are primary.
151
- `array $attributes`: an optional array of HTML attributes.
152
 
153
Two convenience classes extend `action_menu_link`:
154
 
155
- `action_menu_link_primary`: will be added as a primary item.
156
- `action_menu_link_secondary`: will be added as a secondary item.
157
 
158
### `pix_icon`
159
 
160
The action menu can render `pix_icon` as primary actions. The `pix_icon` is a standard output class for generating icons in Moodle.
161
 
162
Construct params:
163
 
164
- `String $pix`: the internal icon location. For example, "t/user".
165
- `String $alt`: an optional alternative text
166
- `String $component`: the pix icon component. By default, only core icons from the `pix` folder will be used
167
- `Array $attributes`: optional HTML attributes.
168
 
169
### `core\output\local\action_menu\subpanel`
170
 
171
The `core\output\local\action_menu` allow the `action_menu` to add items that display subpanels when hovered or clicked.
172
 
173
Construct params:
174
 
175
- `string $text`: text to display in the menu item
176
- `renderable $subpanel`: the output to render inside the subpanel. This param should be renderable using the standard `output::render` method.
177
- `array $attributes` optional HTML attributes
178
 
179
The following example creates a subpanel using a renderable choicelist instance:
180
 
181
{{< php >}}
182
/** @var core_renderer $output*/
183
$output = $PAGE->get_renderer('core');
184
 
185
// A choice list is a renderable class to outpout a user choice.
186
$choice = new core\output\choicelist('Choice example');
187
$choice->add_option("statusa", "Status A", [
188
    'url' => $PAGE->url,
189
    'description' => 'Status A description',
190
    'icon' => new pix_icon('t/user', '', ''),
191
]);
192
$choice->add_option("statusb", "Status B", [
193
    'url' => $PAGE->url,
194
    'description' => 'Status B description',
195
    'icon' => new pix_icon('t/groupv', '', ''),
196
]);
197
$choice->set_selected_value('statusb');
198
 
199
$menu = new action_menu();
200
 
201
// Add subpanel item.
202
$menu->add(new core\output\local\action_menu\subpanel(
203
    'Subpanel example',
204
    $choice
205
));
206
 
207
echo $output->render($menu);
208
{{< / php >}}
209
 
210
### HTML string
211
 
212
If a plain string is added to an action_menu, the action_menu::add method will be printed as it is inside the dropdown menu (as a secondary item).