From ddeec0c02b337cae02c4acdfc19bc4a5c621178d Mon Sep 17 00:00:00 2001 From: Jun Pataleta Date: Tue, 16 Sep 2025 18:22:27 +0800 Subject: [PATCH] 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. --- customfield/classes/output/management.php | 6 +++++- customfield/externallib.php | 2 ++ customfield/templates/list.mustache | 13 +++++++++++-- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/customfield/classes/output/management.php b/customfield/classes/output/management.php index 2fe4cb330f7..c218c16c5e6 100644 --- a/customfield/classes/output/management.php +++ b/customfield/classes/output/management.php @@ -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'); diff --git a/customfield/externallib.php b/customfield/externallib.php index 27a2860331b..f5ce1c68494 100644 --- a/customfield/externallib.php +++ b/customfield/externallib.php @@ -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), ) ); } diff --git a/customfield/templates/list.mustache b/customfield/templates/list.mustache index 6e2965832d8..a7ff2d9da17 100644 --- a/customfield/templates/list.mustache +++ b/customfield/templates/list.mustache @@ -38,6 +38,8 @@ "area": "course", "itemid": 0, "usescategories": 1, + "canmovecategories": 1, + "canmovefields": 1, "categories": [ { "id": "0", "nameeditable": "Other fields", @@ -78,7 +80,9 @@
{{#usescategories}}

- {{> core/drag_handle}} + {{#canmovecategories}} + {{> core/drag_handle}} + {{/canmovecategories}} {{{nameeditable}}} {{#pix}} t/delete, core, {{#str}} delete, moodle {{/str}} {{/pix}} @@ -103,7 +107,12 @@ {{#fields}} - {{> core/drag_handle}}{{{name}}} + + {{#canmovefields}} + {{> core/drag_handle}} + {{/canmovefields}} + {{{name}}} + {{{shortname}}} {{{type}}}