Reputation: 11442
I'm working on a multi-language solution in Sitecore and want to use the DisplayName property of an item to represent the URL to allow for language-specific URLs.
I've set the useDisplayName
web.config property to true as shown below
<linkManager defaultProvider="sitecore">
<providers>
<clear />
<add name="sitecore"
alwaysIncludeServerUrl="false"
encodeNames="true"
type="Sitecore.Links.LinkProvider, Sitecore.Kernel"
addAspxExtension="false"
shortenUrls="true"
languageEmbedding="asNeeded"
languageLocation="filePath"
useDisplayName="true" />
</providers>
</linkManager>
I've also been playing with the <encodeNameReplacements>
section which can replace %20
with a hyphen in the URL to give nice clean URLs - this is done with the following for those who are interested:
<replace mode="on" find=" " replaceWith="-" />
All very good, except that Sitecore breaks if a user enters a hyphen within a DisplayName with the above setting turned on.... If I turn the above setting off, then I have to ensure that users enter nice hyphen separated values for the DisplayName otherwise we start seeing nasty %20
s again in the URL...
So, is there a way to validate the DisplayName property to either disallow or allow hyphens being used?
Or, even better, is there a way to hook into whatever code is executed when the encodeNameReplacements thing happens? This would be ideal, as I could allow users to enter whatever they like for DisplayName, then just sanitise this value on the fly.
Upvotes: 0
Views: 1447
Reputation: 8877
There is a solution for this, but it will require some coding and it's quite complex. I have used this kind of solution in many projects before and it's the only way of solving this, that i'm aware of.
You don't want to really replace spaces with hyphens when saving the item, because it's not user friendly. My solution works at runtime.
First get rid of the <replace>
rule you added.
Then create your own LinkProvider
(inherit from Sitecore's default provider).
Inside the LinkProvider create a method to "normalize" the item's displayname (e.g. replace spaces for hyphens), let's call this method NormalizeDisplayName()
. Make it public and static cause you will need it later.
So now you have managed to let Sitecore replace all spaces with hyphens in links. The rest you can still configure using the default provider options (addAspxExtension="false" useDisplayName="true", etc)
Next up is the ItemResolver
: Sitecore's default ItemResolver is not going to recognize the item path anymore so you are going to add your own ItemResolver to fix this.
Create a class that inherits from Sitecore.Pipelines.HttpRequest.HttpRequestProcessor
and configure it to be used in the <httpRequestBegin>
pipeline after the default ItemResolver.
Now, when the itemresolver is processed you will first split up the requested itempath (let's assume that "/category-name/subitem-name" was requested).
Starting from the siteroot (which can be pulled from Sitecore.Context.Site
), loop through all children while normalizing their item names using your NormalizeDisplayName()
method you created earlier, until you find one that matches the part of your item path.
So in this case, loop through the children of your Home item until you find one that matches normalized displayname "category-name". Then do the same thing for the children of that item until you find the item with normalized displayname "subitem-name".
This way you can resolve the requested item and it will also work if the original displayname already contained hyphens!
I'm sorry that I can't give you complete code examples as it's quite complex and is not limited to just the above things. You also need to think about redirecting if the URL is not properly formatted and make exceptions for master/core database to prevent Sitecore from breaking.
Hope this helps you!
Upvotes: 2
Reputation: 2670
If you see the source in .net reflector (Sitecore.Shell.Framework.Commands.SetDisplayName) there are no pipelines that runs.
You could make a Saving event, making the display name, as you want replacing hypen with a space
public void OnItemSaving(object sender, EventArgs args)
{
Item item = Event.ExtractParameter(args, 0) as Item;
item.Appearance.DisplayName = item.Appearance.DisplayName.Replace("-", " ");
}
Just a quick example of the event
Upvotes: 0