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

后端 未结 2 1524
野的像风
野的像风 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条回答
  • 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<IObjectConstructionTask>().ImplementedBy<FallbackCheckTask>().LifestyleTransient()
                );
            container.Install(new SitecoreInstaller(config));
        }
    

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

    0 讨论(0)
  • 2020-12-18 07:04

    There are few potential issues with provided solution when sitecore 7 (7.2) + IoC + solr + mvc is used.

    When using IoC ex Winsdor please make sure that your Global.asax looks like this one <%@ Application Codebehind="Global.asax.cs" Inherits="Sitecore.ContentSearch.SolrProvider.CastleWindsorIntegration.WindsorApplication" Language="C#" %>. Once, by mistake this file has been changed to <%@ Application Codebehind="Global.asax.cs" Inherits="Merck.Manuals.Web.Global" Language="C#" %> and language fallback was not working. Also the errors we were getting wasn't descriptive as we thought that solr schema is incorrect.

    Code Sitecore.Context.Items["Disable"] = new VersionCountDisabler(); can be added to PreprocessRequestProcessor and it works fine which is better solution that modifying global.asax.

    0 讨论(0)
提交回复
热议问题