In some scenarios, a non-sxa Sitecore instance with a multi-site setup may require separate Link Providers for each site. The non-sxa sitecore instance does not have the feasibility to do it directly. In this article, we will be talking about the simple way to configure a Site-Specific Link Provider. We will create a custom link provider which will work as a link provider switcher.
We have added a custom patch attribute in linkManager setting node to define the fallback link provider. It will be used when the custom site definition attribute "linkProvider" is missing from any site setting. In the example, we kept the standard "sitecore" link manager as the fallback provider. It can be changed as per need.
Search for "linkManager" and validate the changes & order of all link providers. Make sure that switchLinkProvider should be at last.
Step 3: Back-end implementation for Switch Link Provider
Here, we have created a separate helix project for SwitchLinkManger named "DailySitecore.SwitchLinkManager".
Created a new folder named "links" & added a class with the name "SwitchLinkProvider" which is inherited from LinkProvider.
Created a constructor, and added logic to get the fallback provider value from the config file & stored it in a variable.
Created GetContextProvider() method to logically decide the related linkProvider for the context site to be returned.
To get all available linkproviders:
var providerHelper = ServiceLocator.GetRequiredResetableService<Sitecore.Configuration.ProviderHelper<LinkProvider, LinkProviderCollection>>();
To get linkProvider of current site (context site):
var providerName = Sitecore.Context.Site.Properties["linkProvider"];
NOTE:
"LinkManager.Provider" is deprecated in Sitecore 9 so we can use the below code as an alternative to get linkprovider.
var providerHelper = ServiceLocator.GetRequiredResetableService<Sitecore.Configuration.ProviderHelper<LinkProvider, LinkProviderCollection>>();
LinkProvider myLinkProvider = providerHelper?.Value?.Providers["providerName"]
If providerName for the site is set then it will return the linkProvider which contains a key similar to the providerName variable. Otherwise, it will return the fallback link provider using _fallbackProvider value.
Now, override all options of linkprovider and use GetContextProvider to get values.
0 Comments