How to override any component from my plugin in Nopcommerce 4.0

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
6 years ago
Here is my customViewEngine:

  public class CustomViewLocationExpander : IViewLocationExpander
    {
        private const string THEME_KEY = "nop.themename";

        public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations)
        {
            if (context.Values.TryGetValue(THEME_KEY, out string theme))
            {
                viewLocations = new[] {
                        $"/Themes/{theme}/Views/{{1}}/{{0}}.cshtml",
                        $"/Themes/{theme}/Views/Shared/{{0}}.cshtml",
                        $"~/Plugins/misc.pluginName/Themes/{theme}/Views/Shared/Components/{{1}}/{{0}}.cshtml",

                        $"~/Plugins/misc.pluginName/Views/{{1}}/{{0}}.cshtml",
                        $"~/Plugins/misc.pluginName/Views/{{0}}.cshtml"
                    }
                    .Concat(viewLocations);
            }

            return viewLocations;
        }

        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;

            var themeContext = (IThemeContext)context.ActionContext.HttpContext.RequestServices.GetService(typeof(IThemeContext));
            context.Values[THEME_KEY] = themeContext.WorkingThemeName;
        }
    }


But i'm unable to override "Topmenu" Component from my plugin.

Thanks.
6 years ago
Hello,

Please don't duplicate forum post because nop forum is a precious premises.

We as nopcommunity should follow standards.

Here you can find same post more than once although you look forward in following url.

https://www.nopcommerce.com/boards/t/49940/nopcommerce-40-how-can-i-override-nopviewcomponent.aspx.
6 years ago
Here is the solution for your problem
CustomViewExpander.cs code (In older nopcommerce version we are using custom view engine for searching view location for overriding view, in nop 4.0 view expander is used to find or override component )
 public class CustomViewLocationExpander : IViewLocationExpander
    {
        private const string THEME_KEY = "nop.themename";

        public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations)
        {
            if (context.Values.TryGetValue(THEME_KEY, out string theme))
            {
                if (context.ViewName == "Components/TopMenu/Default")
                {
                    viewLocations = new[] {
                        $"~/Plugins/Misc.PluginName/Themes/{theme}/Views/Shared/Components/CustomTopMenu/Default.cshtml",
                    }
                        .Concat(viewLocations);
                }
            }

            return viewLocations;
        }

        public void PopulateValues(ViewLocationExpanderContext context)
        {
           //Nothing to do
        }
    }



NopStartup.cs file code
    
public class NopStartup : INopStartup
    {

        public void ConfigureServices(IServiceCollection services, IConfigurationRoot configuration)
        {
            services.Configure<RazorViewEngineOptions>(options =>
            {
                options.ViewLocationExpanders.Add(new CustomViewLocationExpander());
            });
        }

        public void Configure(IApplicationBuilder application)
        {
        }

        public int Order
        {
            get { return int.MaxValue; }

    }
6 years ago
sk5202 wrote:
Hello,

Please don't duplicate forum post because nop forum is a precious premises.

We as nopcommunity should follow standards.

Here you can find same post more than once although you look forward in following url.

https://www.nopcommerce.com/boards/t/49940/nopcommerce-40-how-can-i-override-nopviewcomponent.aspx.


I want to override invoke method of component.cs file not only component view. Above forum post is for override only component view
6 years ago
Vishalnai wrote:
Hello,

I want to override invoke method of component.cs file not only component view. Above forum post is for override only component view


For override component.cs
you can call another component from CustomTopMenu.cshtml and call your own component (See below line of code for CustomTopMenu.cshtml)

@await Component.InvokeAsync("MyTopMenu")


So while calling nop.web's top menu your CustomTopMenu.cshtml will call as you have override component view and in your CustomTopMenu.cshtml view you can call new component so now your new component's invoke method will called
6 years ago
chetan.bhaliya wrote:
Here is the solution for your problem
CustomViewExpander.cs code (In older nopcommerce version we are using custom view engine for searching view location for overriding view, in nop 4.0 view expander is used to find or override component )
 public class CustomViewLocationExpander : IViewLocationExpander
    {
        private const string THEME_KEY = "nop.themename";

        public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations)
        {
            if (context.Values.TryGetValue(THEME_KEY, out string theme))
            {
                if (context.ViewName == "Components/TopMenu/Default")
                {
                    viewLocations = new[] {
                        $"~/Plugins/Misc.PluginName/Themes/{theme}/Views/Shared/Components/CustomTopMenu/Default.cshtml",
                    }
                        .Concat(viewLocations);
                }
            }

            return viewLocations;
        }

        public void PopulateValues(ViewLocationExpanderContext context)
        {
           //Nothing to do
        }
    }



NopStartup.cs file code
    
public class NopStartup : INopStartup
    {

        public void ConfigureServices(IServiceCollection services, IConfigurationRoot configuration)
        {
            services.Configure<RazorViewEngineOptions>(options =>
            {
                options.ViewLocationExpanders.Add(new CustomViewLocationExpander());
            });
        }

        public void Configure(IApplicationBuilder application)
        {
        }

        public int Order
        {
            get { return int.MaxValue; }

    }


yes it will work.. thank you so much.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.