diff --git a/src/addons/mod/url/services/handlers/module.ts b/src/addons/mod/url/services/handlers/module.ts index d662a57e2..c08f09595 100644 --- a/src/addons/mod/url/services/handlers/module.ts +++ b/src/addons/mod/url/services/handlers/module.ts @@ -107,18 +107,59 @@ export class AddonModUrlModuleHandlerService extends CoreModuleHandlerBase imple if (handlerData.buttons && hideButton !== undefined) { handlerData.buttons[0].hidden = hideButton; + } - if (module.contents && module.contents[0]) { - const icon = AddonModUrl.guessIcon(module.contents[0].fileurl); - - // Calculate the icon to use. - handlerData.icon = CoreCourse.getModuleIconSrc(module.modname, module.modicon, icon); - } + 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 (!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', + ); + + 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]; + } + } + + if (!mainFile) { + return; + } + + const icon = AddonModUrl.guessIcon(mainFile.fileurl); + + // Calculate the icon to use. + return CoreCourse.getModuleIconSrc(module.modname, module.modicon, icon); + } + /** * Returns if contents are loaded to show link button. *