diff --git a/.upgradenotes/MDL-84555-2025031404100029.yml b/.upgradenotes/MDL-84555-2025031404100029.yml new file mode 100644 index 00000000000..7be29027c31 --- /dev/null +++ b/.upgradenotes/MDL-84555-2025031404100029.yml @@ -0,0 +1,14 @@ +issueNumber: MDL-84555 +notes: + core_course: + - message: >- + New core_course\output\activity_icon class to render activity icons with + or without purpose color. This output will centralize the way Moodle + renders activity icons + type: improved + core: + - message: >- + The new PHP enum core\output\local\properties\iconsize can be used to limit + the amount of icons sizes an output component can use. The enum has the + same values available in the theme_boost scss. + type: improved diff --git a/admin/tool/componentlibrary/content/moodle/components/activityicons.md b/admin/tool/componentlibrary/content/moodle/components/activityicons.md index 2a8e393dd8c..394f1c75af4 100644 --- a/admin/tool/componentlibrary/content/moodle/components/activityicons.md +++ b/admin/tool/componentlibrary/content/moodle/components/activityicons.md @@ -9,43 +9,100 @@ tags: - Available - '4.0' - Updated -- '4.4' +- '5.0' --- ## Activity icon types Moodle activity icons are single black SVG icons that are stored in `mod/PLUGINNAME/pix/monologo.svg`. -### Minimal activity icons +## Rendering activity icons + +The `core_course\output\activity_icon` class is used to render activity icons. It can be used in several ways depending on the context. Also, there is the `core_course\activity_icon` template that can be included directly from mustache templates. + +### Rendering the activity plugin icon + +The following example shows how to render the default activity icon: + +{{< php >}} +use core_course\output\activity_icon; +$renderer = \core\di::get(\core\output\renderer_helper::class)->get_core_renderer(); + +$icon = activity_icon::from_modname('quiz'); + +echo $renderer->render($icon); +{{< /php >}} + +By default, the activity icon will be rendered colored with the activity purpose color (see below). + +### Rendering the activity icon from a cm_info object + +Specific activity instances can have their own custom icons. For example, the `mod_resource` displays the MIME type icon for the resource. To render the activity icon from a `cm_info` object, use the static constructor `from_cm_info`. The method will return an instance of `activity_icon` with the icon URL set to the custom icon if necessary. + +It is possible to render the activity icon from a `cm_info` object: + +{{< php >}} +use core_course\output\activity_icon; +$renderer = \core\di::get(\core\output\renderer_helper::class)->get_core_renderer(); +$cminfo = get_fast_modinfo($courseid)->get_cm($cmid); + +$icon = activity_icon::from_cm_info($cminfo); + +echo $renderer->render($icon); +{{< /php >}} + +### Rendering the activity icon in dark color + +There are pages like the gradebook where the activity icons must be rendered in black color for accessibility or usability reasons. The `core_course\output\activity_icon` class has a `set_colourize` method to define if the icon must be colorized or not. + +The following example shows how to render the default activity icon in black: + +{{< php >}} +use core_course\output\activity_icon; +$renderer = \core\di::get(\core\output\renderer_helper::class)->get_core_renderer(); + +$icon = activity_icon::from_modname('quiz') + ->set_colourize(false); + +echo $renderer->render($icon); +{{< /php >}} + +### Set the activity icon size When rendered in a page with limited space the icons will be shown in their original design, for example on the course gradebook where activity show in the grade table header. -> NOTE: The icon is using the ```.icon``` CSS class which limits the maximum width and height. It's recommended to define width and height into the SVG. +The `core_course\output\activity_icon` class has a `set_icon_size` method to define the icon size. The method accepts any value from `core\output\local\properties\iconsize` enum. -{{< example >}} -