Last active
September 21, 2017 02:57
-
-
Save jammykam/daa151abeeac8a7029bf to your computer and use it in GitHub Desktop.
Sitecore Site Specific Link Provider for up to SC 8.1
This file contains 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.Collections.Specialized; | |
using System.Web; | |
using Sitecore.Data.Items; | |
using Sitecore.Diagnostics; | |
using Sitecore.Links; | |
using Sitecore.Web; | |
namespace Sitecore.Custom.Links | |
{ | |
public class SwitchingLinkProvider : Sitecore.Links.LinkProvider | |
{ | |
private const String FALLBACK_ATTRIBUTE_KEY = "fallback"; | |
private const String LINKPROVIDER_ATTRIBUTE_KEY = "linkProvider"; | |
private string Fallback { get; set; } | |
public override void Initialize(string name, NameValueCollection config) | |
{ | |
base.Initialize(name, config); | |
this.Fallback = config[FALLBACK_ATTRIBUTE_KEY]; | |
Assert.IsNotNullOrEmpty(this.Fallback, "fallback"); | |
} | |
private Sitecore.Links.LinkProvider SiteLinkProvider | |
{ | |
get | |
{ | |
var siteLinkProvider = (Sitecore.Context.Site != null) | |
? Sitecore.Context.Site.Properties[LINKPROVIDER_ATTRIBUTE_KEY] : String.Empty; | |
if (String.IsNullOrEmpty(siteLinkProvider)) | |
siteLinkProvider = this.Fallback; | |
return LinkManager.Providers[siteLinkProvider] | |
?? LinkManager.Providers[this.Fallback]; | |
} | |
} | |
#region LinkProvider Options | |
public override bool AddAspxExtension | |
{ | |
get | |
{ | |
return SiteLinkProvider.AddAspxExtension; | |
} | |
} | |
public override bool AlwaysIncludeServerUrl | |
{ | |
get | |
{ | |
return SiteLinkProvider.AlwaysIncludeServerUrl; | |
} | |
} | |
public override bool EncodeNames | |
{ | |
get | |
{ | |
return SiteLinkProvider.EncodeNames; | |
} | |
} | |
public override LanguageEmbedding LanguageEmbedding | |
{ | |
get | |
{ | |
return SiteLinkProvider.LanguageEmbedding; | |
} | |
} | |
public override LanguageLocation LanguageLocation | |
{ | |
get | |
{ | |
return SiteLinkProvider.LanguageLocation; | |
} | |
} | |
public override bool LowercaseUrls | |
{ | |
get | |
{ | |
return SiteLinkProvider.LowercaseUrls; | |
} | |
} | |
public override bool ShortenUrls | |
{ | |
get | |
{ | |
return SiteLinkProvider.ShortenUrls; | |
} | |
} | |
public override bool UseDisplayName | |
{ | |
get | |
{ | |
return SiteLinkProvider.UseDisplayName; | |
} | |
} | |
#endregion | |
#region LinkProvider Methods | |
public override string ExpandDynamicLinks(string text, bool resolveSites) | |
{ | |
return SiteLinkProvider.ExpandDynamicLinks(text, resolveSites); | |
} | |
public override UrlOptions GetDefaultUrlOptions() | |
{ | |
return SiteLinkProvider.GetDefaultUrlOptions(); | |
} | |
public override string GetItemUrl(Item item, UrlOptions options) | |
{ | |
return SiteLinkProvider.GetItemUrl(item, options); | |
} | |
public override string GetDynamicUrl(Item item, LinkUrlOptions options) | |
{ | |
return SiteLinkProvider.GetDynamicUrl(item, options); | |
} | |
public override bool IsDynamicLink(string linkText) | |
{ | |
return SiteLinkProvider.IsDynamicLink(linkText); | |
} | |
public override DynamicLink ParseDynamicLink(string linkText) | |
{ | |
return SiteLinkProvider.ParseDynamicLink(linkText); | |
} | |
public override RequestUrl ParseRequestUrl(HttpRequest request) | |
{ | |
return SiteLinkProvider.ParseRequestUrl(request); | |
} | |
#endregion | |
} | |
} |
This file contains 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
<linkManager> | |
<providers> | |
<add name="provider-site1" type="Sitecore.Links.LinkProvider, Sitecore.Kernel" addAspxExtension="false" languageEmbedding="never" useDisplayName="true" ... /> | |
<add name="provider-site2" type="Sitecore.Custom.LinkProvider, Sitecore.Custom" alwaysIncludeServerUrl="true" languageEmbedding="always" lowercaseUrls="true" /> | |
</providers> | |
</linkManager> | |
<sites> | |
<site name="site1" linkProvider="provider-site1" ... /> | |
<site name="site2" linkProvider="provider-site2" ... /> | |
<site name="site3" ... /> | |
</sites> |
This file contains 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
<linkManager> | |
<patch:attribute name="defaultProvider" value="switcher" /> | |
<providers> | |
<add name="switcher" fallback="sitecore" type="Sitecore.Custom.Links.SwitchingLinkProvider, Sitecore.Custom" /> | |
</providers> | |
</linkManager> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@mrosiak
Thanks for the heads up and the gist. I'll be looking into it.