How to override admin view

Wednesday, December 4, 2024 4:53:33 AM
  • Posted: Tuesday, April 7, 2020 6:26 PM
  • 2
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 :)


0
  • Posted: Wednesday, April 8, 2020 7:29 AM
  • Team
  • 152
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
0
  • Posted: Wednesday, April 8, 2020 8:11 AM
  • 2
Thanks Krzysztof,

I 'll give a try today ! :)

Best Regards
0
  • Posted: Wednesday, April 8, 2020 8:18 AM
  • Team
  • 152
I'm not sure about display order in AdminViewStartup (it should be over or below 100) :)
0
  • Posted: Tuesday, June 22, 2021 7:44 PM
  • 17
Hi guys. How can I override front views in a plugin? It has same solution? What code should I add to the solution for this?
And the second question is how can I override controllers in a plugin? Is that similar to override services?
Thanks
0
  • Posted: Wednesday, June 23, 2021 6:59 AM
  • Team
  • 152
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:

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
  • Posted: Friday, November 29, 2024 11:31 AM
  • 1
I need to override frontend product box view page using plugin in latest grandnode 2.0 please help me
0
back to top
Filters