Hi,
Trying to override an admin view (from a plugin). Any help / guideline / example would be highly appreciated. I'm really lost on how I should do this.
Thank you all :)
How to override admin view
Monday, November 25, 2024 4:15:32 PM
Hi,
First of all you need to prepare class based on IViewLocationExpander:
For example:
using Microsoft.AspNetCore.Mvc.Razor;
using System.Collections.Generic;
using System.Linq;
namespace Grand.Misc.CustomPlugin.Infrastructure
{
public class AdminViewLocationExpander : IViewLocationExpander
{
public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations)
{
if (context.Values.TryGetValue("Admin", out _))
{
viewLocations = new[] {
$"/Plugins/Misc.CustomPlugin/Admin/Views/{{1}}/{{0}}.cshtml",
$"/Plugins/Misc.CustomPlugin/Admin/Views/Shared/{{0}}.cshtml",
}
.Concat(viewLocations);
}
return viewLocations;
}
public void PopulateValues(ViewLocationExpanderContext context)
{
//add view context only for admin
if (context.AreaName?.Equals("Admin") ?? false)
context.Values["Admin"] = "Y";
return;
}
}
}
And next step is register it:
For example:
using Grand.Core.Infrastructure;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace Grand.Misc.CustomPlugin.Infrastructure
{
public class AdminViewStartup : IGrandStartup
{
public void ConfigureServices(IServiceCollection services, IConfiguration configuration)
{
//register admin view expander
services.Configure<RazorViewEngineOptions>(options =>
{
options.ViewLocationExpanders.Add(new AdminViewLocationExpander());
});
}
public void Configure(IApplicationBuilder application)
{
}
public int Order => 102;
}
}
Regards
Krzysztof
First of all you need to prepare class based on IViewLocationExpander:
For example:
using Microsoft.AspNetCore.Mvc.Razor;
using System.Collections.Generic;
using System.Linq;
namespace Grand.Misc.CustomPlugin.Infrastructure
{
public class AdminViewLocationExpander : IViewLocationExpander
{
public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations)
{
if (context.Values.TryGetValue("Admin", out _))
{
viewLocations = new[] {
$"/Plugins/Misc.CustomPlugin/Admin/Views/{{1}}/{{0}}.cshtml",
$"/Plugins/Misc.CustomPlugin/Admin/Views/Shared/{{0}}.cshtml",
}
.Concat(viewLocations);
}
return viewLocations;
}
public void PopulateValues(ViewLocationExpanderContext context)
{
//add view context only for admin
if (context.AreaName?.Equals("Admin") ?? false)
context.Values["Admin"] = "Y";
return;
}
}
}
And next step is register it:
For example:
using Grand.Core.Infrastructure;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace Grand.Misc.CustomPlugin.Infrastructure
{
public class AdminViewStartup : IGrandStartup
{
public void ConfigureServices(IServiceCollection services, IConfiguration configuration)
{
//register admin view expander
services.Configure<RazorViewEngineOptions>(options =>
{
options.ViewLocationExpanders.Add(new AdminViewLocationExpander());
});
}
public void Configure(IApplicationBuilder application)
{
}
public int Order => 102;
}
}
Regards
Krzysztof
0
Hello,
To override some action in the controller I would like to suggest some tips:
- override by routing: example you can find here:
https://github.com/grandnode/SamplePlugins/blob/master/Grand.Plugin.Misc.ExamplePlugin/RouteProvider.cs
- override by using Middleware:
https://github.com/grandnode/SamplePlugins/blob/master/Grand.Plugin.Misc.ExamplePlugin/Middleware/TestMiddleware.cs
(Middleware need to be register)
- override by using IFilterProvider
Example code:
Add new filterprovider:
And you need to register it:
serviceCollection.AddScoped<IFilterProvider, SampleFilterProvider>();
If you find any other idea to override the controller please share it.
Regards
Krzysztof
To override some action in the controller I would like to suggest some tips:
- override by routing: example you can find here:
https://github.com/grandnode/SamplePlugins/blob/master/Grand.Plugin.Misc.ExamplePlugin/RouteProvider.cs
- override by using Middleware:
https://github.com/grandnode/SamplePlugins/blob/master/Grand.Plugin.Misc.ExamplePlugin/Middleware/TestMiddleware.cs
(Middleware need to be register)
- override by using IFilterProvider
Example code:
Add new filterprovider:
public class SampleFilterProvider : IFilterProvider
{
public void OnProvidersExecuted(FilterProviderContext context)
{
if (context.ActionContext.ActionDescriptor.RouteValues["controller"] == "Checkout" &&
context.ActionContext.ActionDescriptor.RouteValues["action"] == "OnePageCheckout")
{
context.Results.Add(new FilterItem(new FilterDescriptor(new SampleFilter(), FilterScope.Global), new SampleFilter()));
}
}
public void OnProvidersExecuting(FilterProviderContext context)
{
return;
}
public int Order => 2005;
}
public class SampleFilter : IActionFilter
{
public void OnActionExecuting(ActionExecutingContext context)
{
context.Result = new new RedirectToActionResult("yourActionName", "yourControllerName", new { });
}
public void OnActionExecuted(ActionExecutedContext context)
{
return;
}
}
And you need to register it:
serviceCollection.AddScoped<IFilterProvider, SampleFilterProvider>();
If you find any other idea to override the controller please share it.
Regards
Krzysztof
1