Enable Themes to support Admin Area overrides.

2 个月 前
Greetings,

Curious if there is scope to allow Themes to provide overrides for Admin Views?
I noted that in ThemeableViewLocationExpander if the following changes were made, it appears to provide the functionality to override the Admin views.


public void PopulateValues(ViewLocationExpanderContext context)
    {
        //no need to add the themeable view locations at all as the administration should not be themeable anyway
        // if (context.AreaName?.Equals(AreaNames.ADMIN) ?? false)
        //     return;

        context.Values[THEME_KEY] = EngineContext.Current.Resolve<IThemeContext>().GetWorkingThemeNameAsync().Result;
    }

public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations)
    {
        // {2} area , {1} controller , {0} action
        if (context.Values.TryGetValue(THEME_KEY, out var theme))
        {
            viewLocations = new[] {
                    $"/Themes/{theme}/Views/{{1}}/{{0}}.cshtml",
                    $"/Themes/{theme}/Views/Shared/{{0}}.cshtml",
                    //Admin area
                    $"/Themes/{theme}/Areas/{{2}}/Views/{{1}}/{{0}}.cshtml",
                    $"/Themes/{theme}/Areas/{{2}}/Views/Shared/{{0}}.cshtml",
                }
                .Concat(viewLocations);
        }
        return viewLocations;
    }


Then adding the custom views to your theme under path  /Themes/{Theme}/Areas/Admin/Views/ path (Along with the _ViewStart and _ViewImports files)

There was a prior discussion (12yrs ago) here but I couldn't see a resolution https://www.nopcommerce.com/en/boards/topic/13861/custom-theme-and-admin-views

If this is useful I can do up a PR.
2 个月 前
Yes you can override and add new Controllers, Services, Factories and Views and use a Custom View Engine in Admin just as in the Frontend
2 个月 前
Yes, this is an old suggestion. See also this issue.
But we still think it's worth leaving it for customization.
2 个月 前
@Yidna,  it was more a question of should this be achievable in the base product, vs having to provide an overriden ThemeableViewLocationExpander/ViewEngine in your plugin/theme.  

Base product provides pluggable themes for views out of the box, natural extension was admin views also.

All good. I see it has already been decided to not progress.
2 个月 前
jafin wrote:
Greetings,

Curious if there is scope to allow Themes to provide overrides for Admin Views?
I noted that in ThemeableViewLocationExpander if the following changes were made, it appears to provide the functionality to override the Admin views.


public void PopulateValues(ViewLocationExpanderContext context)
    {
        //no need to add the themeable view locations at all as the administration should not be themeable anyway
        // if (context.AreaName?.Equals(AreaNames.ADMIN) ?? false)
        //     return;

        context.Values[THEME_KEY] = EngineContext.Current.Resolve<IThemeContext>().GetWorkingThemeNameAsync().Result;
    }

public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations)
    {
        // {2} area , {1} controller , {0} action
        if (context.Values.TryGetValue(THEME_KEY, out var theme))
        {
            viewLocations = new[] {
                    $"/Themes/{theme}/Views/{{1}}/{{0}}.cshtml",
                    $"/Themes/{theme}/Views/Shared/{{0}}.cshtml",
                    //Admin area
                    $"/Themes/{theme}/Areas/{{2}}/Views/{{1}}/{{0}}.cshtml",
                    $"/Themes/{theme}/Areas/{{2}}/Views/Shared/{{0}}.cshtml",
                }
                .Concat(viewLocations);
        }
        return viewLocations;
    }


Then adding the custom views to your theme under path  /Themes/{Theme}/Areas/Admin/Views/ path (Along with the _ViewStart and _ViewImports files)

There was a prior discussion (12yrs ago) here but I couldn't see a resolution https://www.nopcommerce.com/en/boards/topic/13861/custom-theme-and-admin-views

If this is useful I can do up a PR.


You need to do this:


public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations)
    {
        // {2} area , {1} controller , {0} action
        if (context.Values.TryGetValue(THEME_KEY, out var theme))
        {
            viewLocations = new[] {
                    $"/Themes/{theme}/Views/{{1}}/{{0}}.cshtml",
                    $"/Themes/{theme}/Views/Shared/{{0}}.cshtml"
                }
                .Concat(viewLocations);
        }
else if(context.AreaName == "Admin")
{
viewLocations = new[]{
//Admin area
                    $"/Areas/{{2}}/Views/{{1}}/{{0}}.cshtml",
                    $"/Areas/{{2}}/Views/Shared/{{0}}.cshtml"
// If you are using plugin then use below
                   $"/plugins/SystemName/Areas{{2}}/Views/{{1}}/{{0}}.cshtml",
                  $"/plugins/SystemName/Areas/{{2}}/Views/Shared/{{0}}.cshtml"
}
}
        return viewLocations;
    }
2 个月 前
Thanks @SagarKayasth , What I posted is working for me locally. I was more curious about contribution back.