Can Glass.Mapper V3 support language fallback (field-level and item-level)?

后端 未结 2 1533
野的像风
野的像风 2020-12-18 06:14

We just updated our project to use Glass.Mapper V3. We LOVE it. But we\'ve encountered an issue. It doesn\'t seem to respect language fallback.

We have our site set

2条回答
  •  萌比男神i
    2020-12-18 06:53

    I am updating this with a bit of background on why we do the check. If you ask for a Sitecore item that doesn't exist you get a null value, so that is simple to handle. However if you ask for a Sitecore item that doesn't exist in that particular language returns an item with no versions. This means we have to do this check because otherwise Glass would end up returning empty class which I don't think makes much sense.

    This answer will get a little experimental.

    First in the the Spherical.cs file you need to disable the check:

    protected void Application_BeginRequest()
    {
        Sitecore.Context.Items["Disable"] = new VersionCountDisabler();
    }
    

    We can then move the check to later on to the Object Construction pipeline. First create a task:

    public class FallbackCheckTask : IObjectConstructionTask
    {
        public void Execute(ObjectConstructionArgs args)
        {
            if (args.Result == null)
            {
                var scContext = args.AbstractTypeCreationContext as SitecoreTypeCreationContext;
                if (scContext.Item == null)
                {
                    args.AbortPipeline();
                    return;
                }    
                //this checks to see if the item was created by the fallback module
                if (scContext.Item is Sitecore.Data.Managers.StubItem)
                {
    
                    return;
                }
    
                // we could be trying to convert rendering parameters to a glass model, and if so, just return.
                if (String.Compare(scContext.Item.Paths.FullPath, "[orphan]/renderingParameters", true) == 0)
                {
                    return;
                }
    
                if (scContext.Item.Versions.Count == 0)
                {
                    args.AbortPipeline();
                    return;
                }
            }
        }
    }
    

    Then finally register this task in the GlassMapperScCustom class:

        public static void CastleConfig(IWindsorContainer container){
            var config = new Config();
    
            container.Register(
                Component.For().ImplementedBy().LifestyleTransient()
                );
            container.Install(new SitecoreInstaller(config));
        }
    

    I haven't tested this but it should in theory work <- disclaimer ;-)

提交回复
热议问题