Skip to content

Commit

Permalink
MOBILE-4407 url: Fix icon inside url resources
Browse files Browse the repository at this point in the history
  • Loading branch information
crazyserver committed Aug 30, 2023
1 parent f599185 commit 55d126c
Showing 1 changed file with 46 additions and 5 deletions.
51 changes: 46 additions & 5 deletions src/addons/mod/url/services/handlers/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string | undefined> {
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);
}

/**
Expand Down

0 comments on commit 55d126c

Please sign in to comment.