Last active
November 12, 2017 13:10
-
-
Save jammykam/4209306c3c1ef69a0d2e3fa1114e19aa to your computer and use it in GitHub Desktop.
A SwitchingLinkManager for Sitecore 8.2 allowing you to use a different LinkProvider per site
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0"?> | |
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/"> | |
<sitecore> | |
<services> | |
<configurator type="Sitecore.Custom.Providers.ServicesConfigurator, Sitecore.Custom" /> | |
</services> | |
</sitecore> | |
</configuration> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Extensions.DependencyInjection; | |
using Microsoft.Extensions.DependencyInjection.Extensions; | |
using Sitecore.Abstractions; | |
using Sitecore.DependencyInjection; | |
namespace Sitecore.Custom.Providers | |
{ | |
public class ServicesConfigurator : IServicesConfigurator | |
{ | |
public void Configure(IServiceCollection serviceCollection) | |
{ | |
var service = new ServiceDescriptor(typeof(BaseLinkManager), typeof(SwitchingLinkManager), ServiceLifetime.Singleton); | |
serviceCollection.Replace(service); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Web; | |
using Sitecore.Abstractions; | |
using Sitecore.Configuration; | |
using Sitecore.Data.Items; | |
using Sitecore.Diagnostics; | |
using Sitecore.Links; | |
using Sitecore.Sites; | |
using Sitecore.Web; | |
namespace Sitecore.Custom.Providers | |
{ | |
public class SwitchingLinkManager : BaseLinkManager | |
{ | |
private readonly ProviderHelper<LinkProvider, LinkProviderCollection> providerHelper; | |
public SwitchingLinkManager(ProviderHelper<LinkProvider, LinkProviderCollection> providerHelper) | |
{ | |
this.providerHelper = providerHelper; | |
} | |
protected virtual LinkProvider Provider | |
{ | |
get | |
{ | |
var siteLinkProvider = (Sitecore.Context.Site != null) | |
? Sitecore.Context.Site.Properties["linkProvider"] : String.Empty; | |
if (String.IsNullOrEmpty(siteLinkProvider)) | |
return this.providerHelper.Provider; | |
return this.providerHelper.Providers[siteLinkProvider] | |
?? this.providerHelper.Provider; | |
} | |
} | |
/* below is copy/paste from Sitecore.Links.DefaultLinkManager cos Provider is marked internal :'( */ | |
public override bool AddAspxExtension => this.Provider.AddAspxExtension; | |
public override bool AlwaysIncludeServerUrl => this.Provider.AlwaysIncludeServerUrl; | |
public override LanguageEmbedding LanguageEmbedding => this.Provider.LanguageEmbedding; | |
public override LanguageLocation LanguageLocation => this.Provider.LanguageLocation; | |
public override bool LowercaseUrls => this.Provider.LowercaseUrls; | |
public override bool ShortenUrls => this.Provider.ShortenUrls; | |
public override bool UseDisplayName => this.Provider.UseDisplayName; | |
public override string ExpandDynamicLinks(string text) | |
{ | |
Assert.ArgumentNotNull(text, nameof(text)); | |
return this.ExpandDynamicLinks(text, false); | |
} | |
public override string ExpandDynamicLinks(string text, bool resolveSites) | |
{ | |
Assert.ArgumentNotNull(text, nameof(text)); | |
return Assert.ResultNotNull<string>(this.Provider.ExpandDynamicLinks(text, resolveSites)); | |
} | |
public override UrlOptions GetDefaultUrlOptions() | |
{ | |
return Assert.ResultNotNull<UrlOptions>(this.Provider.GetDefaultUrlOptions()); | |
} | |
public override string GetDynamicUrl(Item item) | |
{ | |
return this.GetDynamicUrl(item, LinkUrlOptions.Empty); | |
} | |
public override string GetDynamicUrl(Item item, LinkUrlOptions options) | |
{ | |
return this.Provider.GetDynamicUrl(item, options); | |
} | |
public override string GetItemUrl(Item item) | |
{ | |
return this.Provider.GetItemUrl(item, this.GetDefaultUrlOptions()); | |
} | |
public override string GetItemUrl(Item item, UrlOptions options) | |
{ | |
return this.Provider.GetItemUrl(item, options); | |
} | |
public override bool IsDynamicLink(string linkText) | |
{ | |
return this.Provider.IsDynamicLink(linkText); | |
} | |
public override DynamicLink ParseDynamicLink(string linkText) | |
{ | |
return this.Provider.ParseDynamicLink(linkText); | |
} | |
public override RequestUrl ParseRequestUrl(HttpRequest request) | |
{ | |
return this.Provider.ParseRequestUrl(request); | |
} | |
public override SiteInfo ResolveTargetSite(Item item) | |
{ | |
Assert.ArgumentNotNull(item, nameof(item)); | |
return this.Provider.ResolveTargetSite(item); | |
} | |
public override SiteContext GetPreviewSiteContext(Item item) | |
{ | |
Assert.ArgumentNotNull(item, nameof(item)); | |
return this.Provider.GetPreviewSiteContext(item); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0"?> | |
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/"> | |
<sitecore> | |
<linkManager defaultProvider="default-provider"> | |
<providers> | |
<add name="custom-provider" type="Sitecore.Links.LinkProvider, Sitecore.Kernel" | |
addAspxExtension="true" | |
alwaysIncludeServerUrl="true" | |
encodeNames="true" | |
languageEmbedding="never" | |
languageLocation="filePath" | |
lowercaseUrls="true" | |
shortenUrls="true" | |
useDisplayName="false"/> | |
<add name="alternate-provider" type="MySite.Custom.Links.AlternateProvider, MySite.Custom" ... /> | |
<add name="default-provider" type="MySite.Custom.Links.DefaultProvider, MySite.Custom" ... /> | |
</providers> | |
</linkManager> | |
<sites> | |
<site name="website" | |
set:linkProvider="custom-provider" /> | |
<site name="my-custom-site" | |
inherits="website" | |
linkProvider="alternate-provider" /> | |
</sites> | |
</sitecore> | |
</configuration> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment