How can I override admin area view using plugin?

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
Il y a 3 ans
Hi,
How can I override admin area view using plugin? I have seen many plugin to override front side views and css/js but not to override admin area views or data/core classes. Is any way to do these?
Thanks.
Il y a 3 ans
Hello Atiqiub
Please try this to override view.

Step 1:  create CustomViewLocationExpander.cs file
public class CustomViewLocationExpander : IViewLocationExpander
{
    public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations)
    {
            if (context.AreaName == "Admin" && context.ControllerName == "Product" && context.ViewName == "List")
            {
                viewLocations = new[] {"~/Plugins/Nop.Plugin.YourpluginName/Views/Admin/Product/List.cshtml",
                }.Concat(viewLocations);
            }
        return viewLocations;
    }

    public void PopulateValues(ViewLocationExpanderContext context)
    {
        //
    }
}


Step 2: Create PluginStartup.cs file
public class PluginDbStartup : INopStartup
{
    /// <summary>
    /// Add and configure any of the middleware
    /// </summary>
    /// <param name="services">Collection of service descriptors</param>
    /// <param name="configuration">Configuration of the application</param>
    public void ConfigureServices(IServiceCollection services, IConfiguration configuration)
    {
        // add view location expander
        services.Configure<RazorViewEngineOptions>(options =>
        {
            options.ViewLocationExpanders.Add(new CustomViewLocationExpander());
        });
    }

    /// <summary>
    /// Configure the using of added middleware
    /// </summary>
    /// <param name="application">Builder for configuring an application's request pipeline</param>
    public void Configure(IApplicationBuilder application)
    {
    }

    /// <summary>
    /// Gets order of this startup configuration implementation
    /// </summary>
    public int Order => 0;
}
Il y a 3 ans
But there is no "IViewLocationExpander" interface file exists in solution.
Il y a 3 ans
Hello
Which nopcommerce version you are working?
Il y a 3 ans
I required any example from existing plugin that use change in DB (Add a field in table or add new table) and modify any existing view in admin. So that I can review. Thanks.
Il y a 3 ans
Nop 4.2
Il y a 3 ans
I find DB change in plugin "Nop.Plugin.Shipping.FixedByWeightByTotal". But I also required to override an existing view of admin area.
Il y a 3 ans
None of the stardard plugins use it so can not provide an example
Add the using statment for the definition
This one overrides the OrderSummary in the front - the same principle should apply for Admin

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc.Razor;
using Nop.Core.Data;
using Nop.Core.Infrastructure;
using Nop.Services.Plugins;

namespace Nop.Plugin.Group.Name.ViewEngine
{
    public class CustomViewEngine : IViewLocationExpander
    {
        public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations)
        {
            if (plugin.SystemName == "Group.Name")
            {
                if (context.AreaName == null)
                {
                    if (context.ViewName == "Components/OrderSummary/Default")
                    {
                        viewLocations = new[] {                           $"~/Plugins/Group.Name/Views/Shared/Components/OrderSummary/Default.cshtml"
                                }
                        .Concat(viewLocations);
                    }
                }
            }
            return viewLocations;
        }

        public void PopulateValues(ViewLocationExpanderContext context)
        {
        }
    }
}
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.