Try our conversational search powered by Generative AI!

Htmlhelper to add a link to language specific page from LinkItem

Vote:
 

The pageLink html helper can be used to add links to LinkItems eg. from a LinkItemCollection. But what I have seen, the pageLink helper do not take the language of the LinkItem into account. Which means the link will always be to the preffered culture. In some cases the editors might be interested in adding links to pages in specific languages. 

What I have done to solve this is to make my own html helper, which is pretty much the same as the built in pagelink htmhelper for linkItem but this one will take the language of the LinkItem into account. 

 

public static MvcHtmlString PageLanguageLink(this HtmlHelper htmlHelper, LinkItem item, IPermanentLinkMapper permanentLinkMapper)
        {
            if (permanentLinkMapper == null)
            {
                permanentLinkMapper = ServiceLocator.Current.GetInstance<IPermanentLinkMapper>();
            }
            PermanentLinkMap permanentLinkMap = permanentLinkMapper.Find(new UrlBuilder(item.GetMappedHref()));
            PermanentContentLinkMap permanentContentLinkMap = permanentLinkMap as PermanentContentLinkMap;
            if (permanentContentLinkMap == null)
            {
                return MvcHtmlString.Create(item.ToMappedLink(false));
            }
            object htmlAttributesFromLinkItem = HtmlHelperExtensions.GetHtmlAttributesFromLinkItem(item);

            return htmlHelper.PageLink(item.Text, permanentContentLinkMap.ContentReference, new { language = !string.IsNullOrEmpty(item.Langauge) ? item.Langauge : ContentLanguage.PreferredCulture.Name }, htmlAttributesFromLinkItem, null, null, null);
        }

    

This extension will return a link to the page in the language specified in the Language property in LinkItem. 

#63482
Nov 20, 2012 16:22
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.