How to override view file from plugin and make it compatible with all themes of vendors, without changing anything?

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
6 years ago
timmit wrote:
So your plugin has a Custom View Engine which overrides _ProductAttributes.cshtml

And the custom theme plugin has a Custom View Engine which overrides _ProductAttributes.cshtml

It sounds like you need to make sure your Custom View Engine executes first. I don't have any experience with Custom View Engines, but there should be some way to do this.


No!!
That is not what I'm asking for. I'm overriding view page, let say _ProductAttributes.cshtml from plugin, also add some custom code to that override view. At the same time I'm using some theme which has same page inside.

Now, If I run the application, it will take my view page..as it's overriding existing theme's view page.
But in this case UI of _ProductAttributes.cshtml page will be different from them's design. So end user will have to change designing to make it compatible with theme.

I don't want to do this..so let's think about another case. If I not overriding view page from my plugin, then I'll not able to add my changes to view page (except using action filter, but injecting would not be convenient way to me).

But, I want to run override view page with theme compatibility.
Currently, end user have to manually add my custom code to the theme folder.

So, basically, I'm asking for solution to get rid out of it.
Hope you get it clear now!!
6 years ago
timmit wrote:
Also, don't create duplicate topics -
https://www.nopcommerce.com/boards/t/41772/custom-view-engine-cannot-find-views-in-plugin-if-same-view-exists-in-theme-or-default-view.aspx#200461


Please check on more time, I didn't create any duplicate post.
6 years ago
What you're asking for seems impossible to me.
4 years ago
Hi all

I know this is an old post but I have some info. So the problem is when running a custom bought e.g (Noplite)theme over the default theme that you get with NopCommerce 4.1. Under Nop.Theme.Framework there is another theme folder. In this theme folder they have there own ThemeableViewLocationExpander.cs which uses the IViewLocationExpander.

The problem arises when you create your own CustomViewEngine in a plugin using IViewLocationExpander and add the view location of your view that is in your plugin, this includes registering your CustomeViewWngine using INopStartup. When in debug mode you will see that it will execute your CustomViewEngine first and add your path to your view location list, however just after your CustomView Engine has been executed it executes te themes ThemeableViewLocationExpander.cs in the theme folder as mentioned above. Now your path is not the first path in the list so it automatically goes to the original them path for the view.

This is my CustomViewEngine


IEnumerable<string> IViewLocationExpander.ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations)
        {
            if (context.Values.TryGetValue(THEME_KEY, out string theme))
            {
                if (context.AreaName == null && context.ViewName == "_CreateOrUpdateAddress")
                {
                    viewLocations = new[] { $"~/Plugins/Misc.PluginName/Views/{{0}}.cshtml", }.Concat(viewLocations);

                }
            }

            return viewLocations;
        }




This is the CustomViewEngine under Nop.Web.Framework for the custom theme. This is triggered after my customViewEngine which basically sets the location back to the theme folder.



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",
                    }
                    .Concat(viewLocations);
            }


            return viewLocations;
        }




I still have not found a solution around it. I do know that if you set your theme back to the default theme and remove the custom theme it will override the view in your plugin correctly as I have tested it.

You can modify your Theme's ThemeableViewLocationExpander.cs under nop.web.framework themes folder by adding your locations here and it will load your plugin view however it does not seem to pull all the css styling over.

I thought that this information might assist someone else in coming up with a solution,.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.