diff --git a/src/addons/mod/url/services/handlers/module.ts b/src/addons/mod/url/services/handlers/module.ts index d662a57e28c..c08f0959553 100644 --- a/src/addons/mod/url/services/handlers/module.ts +++ b/src/addons/mod/url/services/handlers/module.ts @@ -107,16 +107,57 @@ export class AddonModUrlModuleHandlerService extends CoreModuleHandlerBase imple if (handlerData.buttons && hideButton !== undefined) { handlerData.buttons[0].hidden = hideButton; + } + + try { + handlerData.icon = await this.getIconSrc(module); + } catch { + // Ignore errors. + } + + return handlerData; + } + + /** + * @inheritdoc + */ + async getIconSrc(module?: CoreCourseModuleData): Promise { + if (!module) { + return; + } + + let mainFile = module.contents?.[0]; - if (module.contents && module.contents[0]) { - const icon = AddonModUrl.guessIcon(module.contents[0].fileurl); + if (!mainFile) { + try { + // Try to get module contents, it's needed to get the URL with parameters. + const contents = await CoreCourse.getModuleContents( + module, + undefined, + undefined, + true, + false, + undefined, + 'url', + ); - // Calculate the icon to use. - handlerData.icon = CoreCourse.getModuleIconSrc(module.modname, module.modicon, icon); + mainFile = contents[0]; + } catch { + // Fallback in case is not prefetched. + const mod = await CoreCourse.getModule(module.id, module.course, undefined, true, false, undefined, 'url'); + + mainFile = mod.contents?.[0]; } } - return handlerData; + if (!mainFile) { + return; + } + + const icon = AddonModUrl.guessIcon(mainFile.fileurl); + + // Calculate the icon to use. + return CoreCourse.getModuleIconSrc(module.modname, module.modicon, icon); } /**