How to detect if a Plugin is installed from a View?

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
12 years ago
What's the best way to detect if a Plugin is installed from a View?
12 years ago
New York wrote:
What's the best way to detect if a Plugin is installed from a View?


Can you elaborate on what exactly you mean? Is this view within your plugin? Is this supposed to be outside the administration section, because within the administration section you will get a list of plugins and their installed/uninstalled status.
12 years ago
Not in plugin or Admin.  For example, I want to create a theme that could support a 3rd party Plugin, but it's not required

Take for example your RequestQuote sample

_ProductVariantPrice.cshtml

    @if ( ...request_quote_plugin_installed...)
    {
        <a title="Request Quote" href="@Url.RouteUrl("Nop.Plugin.Pricing.RequestQuote", ...
    }
    else
    {
        <a href="@Url.RouteUrl("ContactUs")">@T("Products.CallForPrice")</a>
    }
12 years ago
New York wrote:
Not in plugin or Admin.  For example, I want to create a theme that could support a 3rd party Plugin, but it's not required

Take for example your RequestQuote sample

_ProductVariantPrice.cshtml

    @if ( ...request_quote_plugin_installed...)
    {
        <a title="Request Quote" href="@Url.RouteUrl("Nop.Plugin.Pricing.RequestQuote", ...
    }
    else
    {
        <a href="@Url.RouteUrl("ContactUs")">@T("Products.CallForPrice")</a>
    }



In your controller constructor add a parameter IPluginFinder and map it to a readonly field. Inside of your action that returns the view in question add the code below replace "MyViewModel" with the model you are sending to the view. You're going to need to create a new property on your view model. I've called it "IsPluginInstalled" and it is a boolean property.


PluginDescriptor myNewPlugin = _pluginFinder.GetPluginDescriptorBySystemName("NewPluginSystemName");

MyViewModel viewModel = new MyViewModel();
viewModel.IsPluginInstalled = myNewPlugin != null && myNewPlugin.Installed;

return View("ViewName", viewModel);


I hope this helps.

-Sky
12 years ago
I don't have a controller for my view, so I'm relying on the Nop.Web one - and I think that would be the Catalog controller in my example above.  I really don't want to copy/modify that controller because of big risk that it will be updated by nop team.
(maybe I don't understand the nop controllers well enough at this point -  could this be a bit of a flaw in the level of granularity chosen by nop design? - the Catalog controller returns many views)

It may not be best MVC practices, but could I somehow call
_pluginFinder.GetPluginDescriptorBySystemName("NewPluginSystemName")
from inside the view?

It would seem this is a needed feature - could the nop team provide such a "global" function similar to how @T("...") returns localized strings?    (  @P( )  :)
12 years ago
New York wrote:
It would seem this is a needed feature - could the nop team provide such a "global" function similar to how @T("...") returns localized strings?    (  @P( )  :)

There's no need to bloat code just for one function which is not widely used (I mean views). @T is used several thousand times.
12 years ago
a.m. wrote:
It would seem this is a needed feature - could the nop team provide such a "global" function similar to how @T("...") returns localized strings?    (  @P( )  :)
There's no need to bloat code just for one function which is not widely used (I mean views). @T is used several thousand times.


I have to agree with Andrei on this one. Right now there is no demand for such a function and if it were to be created now it would probably experience significant change when developers actually started using it. Also the functionality you're asking for isn't all that appropriate to be put into a view. It should be handled in the controller and exposed via the model.

If you're absolutely required to use a function like this and do not want to use a model then you can create a static method to provide this functionality. However, you should try and make the first example work if at all possible.


public static bool IsPluginInstalled(string systemName){
PluginDescriptor myNewPlugin = EngineContext.Current.Resolve<IPluginFinder>().GetPluginDescriptorBySystemName(systemName);

return myNewPlugin != null && myNewPlugin.Installed;
}
12 years ago
I was being a bit faceious regarding @P().  I don't expect that IsPluginInstalled() would be used in html context, but rather as @if(... )

However, I respectfully disagree with your comments for several reasons

1) This is a "helper" function that taps into nop framework; it's not related to a "business" entity/concern.
IMHO Is...PluginInstalled just does not belong in a model, and in the case of a Theme just needing to adjust the presentation, I think my point regarding risk above has merit.

2) I don't see it being much different in concept to html helper functions, e.g.
   _ColumnsThree
      @if (IsSectionDefined("left"))
except that it's just a nop framework helper function
    
2) I don't understand the "bloat".  This is just a 2-line function

3) There are several nop Views that have "code", and I suspect there were good reasons not to put that code into a controller & add to the model.  E.g.

  _Root  
  @{
    var storeLocation = EngineContext.Current.Resolve<Nop.Core.IWebHelper>().GetStoreLocation();
    var displayMiniProfiler = EngineContext.Current.Resolve<Nop.Core.Domain.StoreInformationSettings>().DisplayMiniProfilerInPublicStore;
  }

4) re: no demand ... would probably experience significant change
There is yet no demand because developers have not really started creating plugins and themes - there are very few in the Extensions https://www.nopcommerce.com/extensions.aspx for Nop 2 - two payment, and your sample RequestQuote.
I don't see why an IsPluginInstalled() would ever experience change.
I would think as more Plugins and Themes are developed, they could potentially interact with other Plugins; knowing if that other plugin is available is useful.

I decided for now just to put the code into my View:

@using Nop.Core.Plugins
@{
    PluginDescriptor requestQuotePlugin = EngineContext.Current.Resolve<IPluginFinder>().GetPluginDescriptorBySystemName("Pricing.RequestQuote");        
    bool IsRequestQuotePluginInstalled =  requestQuotePlugin != null && requestQuotePlugin.Installed;    
}

...

        if (IsRequestQuotePluginInstalled)
        {        
            <a class="productPrice" title="Request Quote" href="@Url.RouteUrl("Nop.Plugin.Pricing.RequestQuote", new { productVariantId = Model.ProductVariantId })">Request a Quote!</a>
        }
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.