EPiServer:Property from other language

Vote:
 

I'm using CMS 6 and I'm trying to fetch data from a different language branch then the current one with the Property control. I thought that PageReference contained something that described what language to get but I can't find it.

Anyone have some ideas on how to do this?

#49195
Mar 09, 2011 10:22
Vote:
 

I don't think there's any easier way to do this than to set the InnerProperty property with the PropertyData of the desired language.

#49196
Mar 09, 2011 10:55
Vote:
 

The trick is to make your own proeprty get hanbdler

PropertyDataCollection.GetHandler = EPiFixes.FetchDataFrom.OwnPropertyHandler;//override Property["xyz"]

  Here is my class where I override it. Think there is a lot more code here than you need, but you will see the trick

   public static class Helper
    {
        public static PageData GetOwnerPage(this PropertyDataCollection properties)
        {
            if (properties != null)
            {
                PropertyInfo info = properties.GetType().GetProperty("OwnerPage",BindingFlags.NonPublic|BindingFlags.Instance);
                var result = info.GetValue(properties, null) as PageData;
                if (result == null)
                {
                    //Trying to get page
                    PropertyData p1 = properties.Get("PageLink");
                    PropertyData p2 = properties.Get("PageLanguageBranch");
                    if (p1 != null && p2 != null && p1 is PropertyPageReference && !(p1 as PropertyPageReference).IsNull)
                    {
                        result = EPiServer.DataFactory.Instance.GetPage((p1 as PropertyPageReference).PageLink, new LanguageSelector(p2.ToString()));
                    }
                }
                return result;
            }
            return null;
        }
        
    }
    public class FetchDataFrom
    {
        public static PropertyData OwnPropertyHandler(string name, PropertyDataCollection properties)
        {
            return OwnPropertyHandler(name, properties, null);
        }
      
        public static void DefaultPropertyHandlerDebug(string name, PropertyDataCollection properties,StringBuilder debug)
        {
            PropertyData data = properties.Get(name);

            if (data == null || data.IsNull)
            {
                debug.AppendLine("Own property is null");
            }
            else
            {
                debug.AppendLine("Found data in own property");
                PageData ownerPage = properties.GetOwnerPage();
                if (data.IsLanguageSpecific && ownerPage.LanguageID != ownerPage.MasterLanguageBranch)
                {
                    debug.AppendLine("inherits form master lang " + ownerPage.MasterLanguageBranch);
                }
            }
            if ((data == null) || (data.IsNull && !data.IsMetaData))
            {
                PageData ownerPage = properties.GetOwnerPage();
                if (((data != null) && !data.IsLanguageSpecific) && ((ownerPage != null) && !ownerPage.IsMasterLanguageBranch))
                {
                    debug.Append("Language spesific value, returns master lang " + ownerPage.MasterLanguageBranch);
                    return;// ownerPage.GetPageLanguage(ownerPage.MasterLanguageBranch).Property.Get(name);
                }
                PropertyData data2 = FetchDataFromDebug(name, properties,debug);
                if ((data2 != null) && (data2.Value != null))
                {
                    return;// data2;
                }
                //PropertyData data3 = DynamicPropertyTree.Instance.FindDynamicProperty(name, properties);
                //if (data3 != null)
                //{
                //    return data3;
                //}
            }
            
            return;// data;
        }

        public static PropertyData FetchDataFromDebug(string name, PropertyDataCollection properties, StringBuilder debug)
        {
            PropertyData data = properties.Get("PageShortcutType");
            if (((data == null) || (data.Value == null)) || (((PageShortcutType)data.Value) != PageShortcutType.FetchData))
            {
                return null;
            }
            data = properties.Get("PageShortcutLink");
            if (data == null)
            {
                return null;
            }
            PageReference pageLink = (PageReference)data.Value;
            if (!PageReference.IsValue(pageLink))
            {
                return null;
            }
            string str = properties.Get("PageLanguageBranch").Value as string;
            if (string.IsNullOrEmpty(str))
            {
                debug.AppendLine("Geting from " + pageLink);
                return DataFactory.Instance.GetPage(pageLink).Property.Get(name);
            }
            PageData p=DataFactory.Instance.GetPage(pageLink, LanguageSelector.Fallback(str, true));
              debug.AppendLine("Geting from " +p.PageLink+" "+p.LanguageID);

              return p.Property.Get(name);
        }



        public static PageData GetInheritPage(PropertyDataCollection properties,bool allwaysMaster)
        {
             PropertyData data = properties.Get("PageShortcutType");
             if (((data == null) || (data.Value == null)) ||
                 (((PageShortcutType)data.Value) != PageShortcutType.FetchData))
                 return null;

            data = properties.Get("PageShortcutLink");
            if (data == null)
                return null;

            PageReference pageLink = (PageReference)data.Value;
            if (!PageReference.IsValue(pageLink))
                return null;

            if (properties.Get("PageLink") != null && (properties.Get("PageLink") is PropertyPageReference))
            {
                if (pageLink.CompareToIgnoreWorkID((properties.Get("PageLink") as PropertyPageReference).PageLink))
                    return null;
            }
            if (allwaysMaster)
                return DataFactory.Instance.GetPage(pageLink, LanguageSelector.MasterLanguage());
             string str = properties.Get("PageLanguageBranch").Value as string;
             if (string.IsNullOrEmpty(str))
             {
               
                 return DataFactory.Instance.GetPage(pageLink);
             }
             return DataFactory.Instance.GetPage(pageLink, LanguageSelector.Fallback(str, true));
              
                    
        }
        public static PropertyData GetFromMe_ThenCheckFetchFromLocalThenMaster_ElseCheckMasterLang(string name,PropertyDataCollection properties, StringBuilder debug)
        {
            PropertyData data = null;
            if (debug != null)
                debug.AppendLine("GetFromMe_ThenCheckFetchFromLocalThenMaster_ElseCheckMasterLang");
            PageData page = GetInheritPage(properties, false);// DataFactory.Instance.GetPage(pageLink, LanguageSelector.MasterLanguage());
            if (page != null)
            {
                #region check Inherit
                if (debug != null)
                    debug.AppendLine("Getting from " + page.PageLink + " " + page.LanguageID);
                data = page.Property.Get(name);
                if (data != null && data.IsNull && !page.IsMasterLanguageBranch)
                {
                    page = page.GetPageLanguage(page.MasterLanguageBranch);
                    if (page != null)
                    {
                        if (debug != null)
                            debug.AppendLine("is null in local lang getting from master from " + page.PageLink + " " + page.LanguageID);

                        return page.Property.Get(name);
                    }
                }
                else
                {
                    return page.Property.Get(name);
                }
                #endregion
            }
            else
            {

                PageData ownerPage = properties.GetOwnerPage();
                if (debug != null)
                    debug.AppendLine("Checks if this page is a lang version");
                if (ownerPage != null)
                {
                    if (debug != null)
                        debug.AppendLine("ownerPage.IsMasterLanguageBranch=" + ownerPage.IsMasterLanguageBranch);

                    if (!ownerPage.IsMasterLanguageBranch)
                    {
                        page = EPiServer.DataFactory.Instance.GetPage(new PageReference(ownerPage.PageLink.ID), LanguageSelector.MasterLanguage());
                        if (page != null)
                        {
                            if (debug != null)
                                debug.AppendLine("is local lang getting from master from " + page.PageLink + " " + page.LanguageID);

                            return page.Property.Get(name);
                        }
                    }
                }
                else
                {
                    if (debug != null)
                        debug.AppendLine("Owner page is null");
                }

            }
            return data;
        }
        public static PropertyData OwnPropertyHandler(string name, PropertyDataCollection properties,StringBuilder debug)
        {
           
            PropertyData prop = PropertyGetHandler.DefaultPropertyHandler(name, properties);
            if (debug != null)
            {
                
                DefaultPropertyHandlerDebug(name, properties, debug);
            }
           PropertyData data=null;

            if (prop != null && prop.IsNull && !prop.IsDynamicProperty)
            {
                //Lagt til så man ikke får engelsk ingress på franske sider
              
                if (prop.IsLanguageSpecific)
                {
                    if (debug != null)
                        debug.AppendLine("IsLanguageSpecific");
                    string checkName = "PageStopPublish";
                    if (name.Equals(checkName))
                    {
                        PropertyData prop2 = GetFromMe_ThenCheckFetchFromLocalThenMaster_ElseCheckMasterLang(name, properties, debug);
                        if (prop2 != null)
                            return prop2;
                      
                    }
                }
                else
                {
                    if (debug != null)
                        debug.AppendLine("Not LanguageSpecific");
                   
                    PageData page = GetInheritPage(properties, true);// DataFactory.Instance.GetPage(pageLink, LanguageSelector.MasterLanguage());
                    
                    if (page != null)
                    {
                        if (debug != null)
                            debug.AppendLine("Getting from " + page.PageLink + " " + page.LanguageID);
                   

                        data = page.Property.Get(name);
                        if (data != null)
                            return data;
                    }
                }
            }
            return prop;
        }

        public static PropertyData OwnPropertyHandler(string name, PropertyDataCollection properties, PageData thisPage,
                                                      out PageData resultPage, int maxNrOfLevels)
        {
            resultPage = thisPage;
            PropertyData data = properties.Get(name);

           

            if (data != null && !data.IsNull)
                return data;

            if (maxNrOfLevels < 0)
            {
                return data;
            }
            if ((data == null) || (data.IsNull && !data.IsMetaData))
            {
                if (thisPage != null)
                {
                    if (data != null && !thisPage.IsMasterLanguageBranch && !data.IsLanguageSpecific)
                    {
                        resultPage = thisPage.GetPageLanguage(thisPage.MasterLanguageBranch);
                        return OwnPropertyHandler(name, resultPage.Property, resultPage, out resultPage,
                                                  maxNrOfLevels - 1);
                    }
                }
                else
                {
                    if (data != null && !data.IsLanguageSpecific)
                    {
                        if (!properties.GetIsMasterLanguageBranch() &&
                            !PageReference.IsNullOrEmpty(properties.GetPageLink()))
                        {
                            resultPage =
                                DataFactory.Instance.GetPageProvider(
                                    properties.GetPageLink())
                                    .GetPage(properties.GetPageLink(),
                                             new LanguageSelector(properties.GetMasterLanguageBranch()));
                            return OwnPropertyHandler(name, resultPage.Property, resultPage, out resultPage,
                                                      maxNrOfLevels - 1);
                        }
                    }
                }
                if (thisPage == null)
                {
                    if (properties.Get("PageLink") != null)
                    {
                        PropertyData data3 = DynamicPropertyTree.Instance.FindDynamicProperty(name, properties);
                        data = FindDynamicProperty(name, out resultPage,
                                                   PageReference.Parse(properties.Get("PageLink").ToString()),
                                                   properties);
                        if (data != null)
                            return data;
                    }
                }

                data = FetchDataFromPage(name, properties, out resultPage, maxNrOfLevels - 1);
                if ((data != null) && (data.IsNull))
                {
                    return data;
                }
                if (data != null)
                {
                    //Fix to ensure that we get something from master lang for images
                    if (name == "SideBarImages" && !properties.GetIsMasterLanguageBranch() &&
                        !PageReference.IsNullOrEmpty(properties.GetPageLink()))
                    {
                        resultPage =
                            DataFactory.Instance.GetPageProvider(
                                properties.GetPageLink())
                                .GetPage(properties.GetPageLink(),
                                         new LanguageSelector(properties.GetMasterLanguageBranch()));
                        return OwnPropertyHandler(name, resultPage.Property, resultPage, out resultPage,
                                                  maxNrOfLevels - 1);
                    }
                }
            }

            return data;
        }


        public static PropertyData FindDynamicProperty(string name, out PageData pageOut, PageReference pageRef,
                                                       PropertyDataCollection properties)
        {
            object o = DynamicPropertyTree.Instance.FindDynamicProperty(name, properties);
            PageReference pageLink = pageRef;
            pageOut = null;
            while (true)
            {
                if (PageReference.IsNullOrEmpty(pageLink))
                    return null;
                DynamicPropertyPage page = (DynamicPropertyPage) DynamicPropertyTree.Instance[pageLink];
                if (page == null)
                {
                    PageData p = DataFactory.Instance.GetPage(pageLink, LanguageSelector.MasterLanguage());
                    if (p == null || PageReference.IsNullOrEmpty(p.ParentLink))
                        return null;
                    pageLink = p.ParentLink;
                }
                else
                {
                    PropertyData property = page.GetProperty(name, properties);
                    if (property != null)
                    {
                        pageOut = EPiServer.DataFactory.Instance.GetPage(page.PageLink);
                        return property;
                    }
                    pageLink = page.Parent;
                }
            }
        }


        public static PropertyData FetchDataFromPage(string name, PropertyDataCollection properties,
                                                     out PageData resultPage, int maxNrOfLevels)
        {
            PropertyData data = properties.Get("PageShortcutType");
            if (((data == null) || (data.Value == null)) ||
                (((PageShortcutType) data.Value) != PageShortcutType.FetchData))
            {
                resultPage = null;
                return null;
            }
            data = properties.Get("PageShortcutLink");
            if (data == null)
            {
                resultPage = null;
                return null;
            }
            PageReference pageLink = (PageReference) data.Value;
            if (!PageReference.IsValue(pageLink))
            {
                resultPage = null;
                return null;
            }
            string str = properties.Get("PageLanguageBranch").Value as string;
            if (string.IsNullOrEmpty(str))
            {
                resultPage = DataFactory.Instance.GetPage(pageLink);
                return OwnPropertyHandler(name, resultPage.Property, resultPage, out resultPage, maxNrOfLevels - 1);
            }
            resultPage = DataFactory.Instance.GetPage(pageLink, LanguageSelector.Fallback(str, true));
            return OwnPropertyHandler(name, resultPage.Property, resultPage, out resultPage, maxNrOfLevels - 1);
        }
    }

    public static class PropertyExtensions
    {
        public static bool GetIsMasterLanguageBranch(this PropertyDataCollection properties)
        {
            if (properties.Get("PageMasterLanguageBranch") != null && properties.Get("LanguageBranch") != null &&
                properties.Get("PageLink") != null)
            {
                return string.Equals(properties.Get("PageMasterLanguageBranch").ToString(),
                                     properties.Get("LanguageBranch").ToString(), StringComparison.OrdinalIgnoreCase);
            }
            return false;
        }

        public static string GetLanguageBranch(this PropertyDataCollection properties)
        {
            if (properties.Get("LanguageBranch") == null)
                return null;
            return (string) properties.Get("LanguageBranch").Value;
        }

        public static string GetMasterLanguageBranch(this PropertyDataCollection properties)
        {
            if (properties.Get("PageMasterLanguageBranch") == null)
                return null;
            return (string) properties.Get("PageMasterLanguageBranch").Value;
        }

        public static PageReference GetPageLink(this PropertyDataCollection properties)
        {
            if (properties.Get("PageLink") == null)
                return PageReference.EmptyReference;
            PageReference reference = (PageReference) properties.Get("PageLink").Value;
            if (reference == null)
            {
                return PageReference.EmptyReference;
            }
            return reference;
        }
    }

    

#49219
Mar 09, 2011 12:13
Vote:
 

More fun suggestions:

You could also the PageLoader of the Property control to one you implement which returns a page of a specified language in its CurrentPage property.

You could put the property inside a PageList control and get the correct PageData (with correct language), put it in a PageDataCollection and use that as DataSource to the PageList :)

#49223
Mar 09, 2011 13:53
Vote:
 

Like your idee Magnus, but the underline problem here is the defult implementasion of GetProperty. That can't handle diffrent languages.

Agree that my code is a bit massive :), but of you remove most of it is should suite your purpoos perfectly. Good luck

#49224
Mar 09, 2011 14:05
Vote:
 

Maybe I was a bit quick, I haven't actually tried any of my suggested approaches.

#49225
Mar 09, 2011 14:46
Vote:
 

The PageList idea worked though :) The below snippet gets the MainBody property from the en language branch no matter what language is being displayed in the rest of the page:

<script runat="server">
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        var pages = new EPiServer.Core.PageDataCollection();
        pages.Add(EPiServer.DataFactory.Instance.GetPage(
                CurrentPage.PageLink, new EPiServer.Core.LanguageSelector("en")));
        plLang.DataSource = pages;          
    }
</script>
<EPiServer:PageList runat="server" ID="plLang">
<ItemTemplate>
    <EPiServer:Property runat="server" PropertyName="MainBody" />
</ItemTemplate>
</EPiServer:PageList>

This should of course be tidied up and perhaps put in a user control which you feed the property name and language branch. 

#49226
Mar 09, 2011 15:01
Vote:
 

I thought the problem was that if you use fetch data from a english page, and the page you are looking at is swedish you will not  get the english values since the fetch data from gets data from the source page on swedish

 

#49227
Edited, Mar 09, 2011 15:05
Vote:
 

Thank you both for the help! I used the PageList-method and it works great. Thank you!

#49268
Mar 10, 2011 15:40
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* 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.