MDL-70158 tool_templatelibrary: group and sort list of components.
authorPaul Holden <paulh@moodle.com>
Mon, 27 Jul 2020 19:53:49 +0000 (20:53 +0100)
committerPaul Holden <paulh@moodle.com>
Tue, 17 Nov 2020 13:56:01 +0000 (13:56 +0000)
admin/tool/templatelibrary/classes/output/list_templates_page.php
admin/tool/templatelibrary/lang/en/tool_templatelibrary.php
admin/tool/templatelibrary/templates/list_templates_page.mustache

index cecf9cb..ebd4500 100644 (file)
@@ -27,6 +27,8 @@ use renderable;
 use templatable;
 use renderer_base;
 use stdClass;
+use core_collator;
+use core_component;
 use core_plugin_manager;
 use tool_templatelibrary\api;
 
@@ -44,29 +46,47 @@ class list_templates_page implements renderable, templatable {
      * @return stdClass
      */
     public function export_for_template(renderer_base $output) {
-        $data = new stdClass();
-        $data->allcomponents = array();
         $fulltemplatenames = api::list_templates();
         $pluginmanager = core_plugin_manager::instance();
-        $components = array();
+        $components = [];
 
         foreach ($fulltemplatenames as $templatename) {
-            list($component, $templatename) = explode('/', $templatename, 2);
-            $components[$component] = 1;
-        }
+            [$component, ] = explode('/', $templatename, 2);
+            [$type, ] = core_component::normalize_component($component);
+
+            // Core sub-systems are grouped together and are denoted by a distinct lang string.
+            $coresubsystem = (strpos($component, 'core') === 0);
+
+            if (!array_key_exists($type, $components)) {
+                $typename = $coresubsystem
+                    ? get_string('core', 'tool_templatelibrary')
+                    : $pluginmanager->plugintype_name_plural($type);
 
-        $components = array_keys($components);
-        foreach ($components as $component) {
-            $info = new stdClass();
-            $info->component = $component;
-            if (strpos($component, 'core') === 0) {
-                $info->name = get_string('coresubsystem', 'tool_templatelibrary', $component);
-            } else {
-                $info->name = $pluginmanager->plugin_name($component);
+                $components[$type] = (object) [
+                    'type' => $typename,
+                    'plugins' => [],
+                ];
             }
-            $data->allcomponents[] = $info;
+
+            $pluginname = $coresubsystem
+                ? get_string('coresubsystem', 'tool_templatelibrary', $component)
+                : $pluginmanager->plugin_name($component);
+
+            $components[$type]->plugins[$component] = (object) [
+                'name' => $pluginname,
+                'component' => $component,
+            ];
         }
 
-        return $data;
+        // Sort returned components according to their type, followed by name.
+        core_collator::asort_objects_by_property($components, 'type');
+        array_walk($components, function(stdClass $component) {
+            core_collator::asort_objects_by_property($component->plugins, 'name');
+            $component->plugins = array_values($component->plugins);
+        });
+
+        return (object) [
+            'allcomponents' => array_values($components),
+        ];
     }
 }
index 4f3134b..e62b529 100644 (file)
@@ -24,6 +24,7 @@
 
 $string['all'] = 'All components';
 $string['component'] = 'Component';
+$string['core'] = 'Core';
 $string['coresubsystem'] = 'Subsystem ({$a})';
 $string['documentation'] = 'Documentation';
 $string['example'] = 'Example';
index c5a454e..71d8042 100644 (file)
             <select id="selectcomponent" class="form-control" data-field="component">
                 <option value="">{{#str}}all, tool_templatelibrary{{/str}}</option>
                 {{#allcomponents}}
-                    <option value="{{component}}">{{name}}</option>
+                    <optgroup label="{{type}}">
+                        {{#plugins}}
+                            <option value="{{component}}">{{name}}</option>
+                        {{/plugins}}
+                    </optgroup>
                 {{/allcomponents}}
             </select>
         {{/element}}