Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve companion module and dependencies in ModuleViewLocationExpander #1079

Closed
Algorithman opened this issue May 1, 2024 · 1 comment
Closed

Comments

@Algorithman
Copy link
Contributor

Algorithman commented May 1, 2024

I made 2 Modules for our shop.
First one (MyCodeModule) is for Controllers, Domain, WebApi etc and the second one is for solely Theming (Theme WBlue).

What I now encountered (after having a typo) is this:

InvalidOperationException: The view 'Index' was not found. The following locations were searched:
/Modules/MyCodeModule/Views/Spareparts/Index.cshtml
/Modules/MyCodeModule/Views/Shared/Index.cshtml
/Themes/WBlue/Views/Spareparts/Index.cshtml
/Themes/WBlue/Views/Shared/Index.cshtml
/Themes/Flex/Views/Spareparts/Index.cshtml
/Themes/Flex/Views/Shared/Index.cshtml
/Views/Spareparts/Index.cshtml
/Views/Shared/Index.cshtml

My Question:
Shouldn't the MyCodeModule paths be checked after the themes?
Because at the moment I can not theme a controller/cshtml defined in MyCodeModule in my WBlue theme. Although the _custom.css of the theme is used.

It doesn't pose a problem for me since both modules are under my control but in other constellations one might want to be able to theme a view of another module.

Btw, I did set Order and DepdendsOn in the module.json properly so the theme depends on the code module. But the code in the IViewLocationExpanders doesn't account for any dependency sorting.

@Algorithman
Copy link
Contributor Author

This works for me in the ModuleViewLocationExpander:

        public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations)
        {
            if (context.Values.TryGetValue(ParamKey, out var moduleName))
            {
                var module = _moduleCatalog.GetModuleByName(moduleName);

                if (module != null)
                {

                    // Check if there are dependencies for this module in the current theme module
                    if (context.Values.TryGetValue(ThemeKey, out var themeName))
                    {
                        var themeModule = _moduleCatalog.Modules.FirstOrDefault(x => x.Theme == themeName);
                        if (themeModule != null && themeModule.DependsOn.Contains(module.SystemName))
                        {
                            var themeRegistry = context.ActionContext.HttpContext.RequestServices.GetRequiredService<IThemeRegistry>();
                            var theme = themeRegistry.GetThemeDescriptor(themeName);

                            var combinedViewLocations = new[]
                            {
                                $"{theme.Path}Views/{{1}}/{{0}}" + RazorViewEngine.ViewExtension,
                                $"{theme.Path}Views/Shared/{{0}}" + RazorViewEngine.ViewExtension,
                                $"{module.Path}Views/{{1}}/{{0}}" + RazorViewEngine.ViewExtension,
                                $"{module.Path}Views/Shared/{{0}}" + RazorViewEngine.ViewExtension,
                            };
                            return combinedViewLocations.Union(viewLocations);
                        }
                    }

                    var moduleViewLocations = new[]
                    {
                        $"{module.Path}Views/{{1}}/{{0}}" + RazorViewEngine.ViewExtension,
                        $"{module.Path}Views/Shared/{{0}}" + RazorViewEngine.ViewExtension,
                    };

                    return moduleViewLocations.Union(viewLocations);
                }
            }

            return viewLocations;
        }

@muratcakir muratcakir changed the title Question about the order of view search paths Resolve companion module and dependencies in ModuleViewLocationExpander May 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants