MDL-86654 customfield: Show drag handles only when necessary

* Show category drag handles when there are 2 or more categories
* Show custom field drag handles when there are a total of 2 or more
fields on the page.
This commit is contained in:
Jun Pataleta
2025-09-16 18:22:27 +08:00
parent 1b2d270397
commit ddeec0c02b
3 changed files with 18 additions and 3 deletions
+5 -1
View File
@@ -76,7 +76,7 @@ class management implements renderable, templatable {
$categories = $this->handler->get_categories_with_fields();
$categoriesarray = array();
$fieldscount = 0;
foreach ($categories as $category) {
$categoryarray = array();
@@ -97,6 +97,7 @@ class management implements renderable, templatable {
$fieldarray['movetitle'] = get_string('movefield', 'core_customfield', $fieldname);
$categoryarray['fields'][] = $fieldarray;
$fieldscount++;
}
$menu = new \action_menu();
@@ -116,6 +117,9 @@ class management implements renderable, templatable {
}
$data->categories = $categoriesarray;
$data->canmovecategories = count($data->categories) > 1;
// Can move fields if there are more than one field or if there are multiple categories.
$data->canmovefields = $fieldscount > 1 || $data->canmovecategories;
if (empty($data->categories)) {
$data->nocategories = get_string('nocategories', 'core_customfield');
+2
View File
@@ -133,6 +133,8 @@ class core_customfield_external extends external_api {
)
)
),
'canmovefields' => new external_value(PARAM_BOOL, 'Whether fields can be moved', VALUE_DEFAULT, false),
'canmovecategories' => new external_value(PARAM_BOOL, 'Whether categories can be moved', VALUE_DEFAULT, false),
)
);
}
+11 -2
View File
@@ -38,6 +38,8 @@
"area": "course",
"itemid": 0,
"usescategories": 1,
"canmovecategories": 1,
"canmovefields": 1,
"categories": [
{ "id": "0",
"nameeditable": "Other fields",
@@ -78,7 +80,9 @@
<div class="col-6 categoryinstance">
{{#usescategories}}
<h3>
<span class="movecategory icon-size-3">{{> core/drag_handle}}</span>
{{#canmovecategories}}
<span class="movecategory icon-size-3">{{> core/drag_handle}}</span>
{{/canmovecategories}}
{{{nameeditable}}}
<a class="icon-size-3" href="#" data-role="deletecategory" data-id="{{id}}">
{{#pix}} t/delete, core, {{#str}} delete, moodle {{/str}} {{/pix}}
@@ -103,7 +107,12 @@
<tbody>
{{#fields}}
<tr data-field-name="{{name}}" data-field-id="{{id}}" class="field">
<td class="col-5"><span class="movefield">{{> core/drag_handle}}</span>{{{name}}}</td>
<td class="col-5">
{{#canmovefields}}
<span class="movefield">{{> core/drag_handle}}</span>
{{/canmovefields}}
{{{name}}}
</td>
<td class="col-3">{{{shortname}}}</td>
<td class="col-2">{{{type}}}</td>
<td class="col-2 text-end">