Glass.Mapper V3是否可以支持语言回退(字段级别和项目级别)? [英] Can Glass.Mapper V3 support language fallback (field-level and item-level)?

查看:82
本文介绍了Glass.Mapper V3是否可以支持语言回退(字段级别和项目级别)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们刚刚更新了项目以使用Glass.Mapper V3.我们喜欢它.但是我们遇到了一个问题.它似乎不尊重语言的回退.

我们已经建立了网站,以便如果用户选择非默认语言,则他们将看到该语言的项目(如果存在).如果没有,他们将看到默认(后备")语言版本.我们还在字段级别进行了设置,因此,如果某项具有非默认版本,但并非所有字段都已更改,则任何未更改的字段都将恢复为该字段的默认语言版本的值.

我们可以做些什么来使Glass使用语言后备吗?

解决方案

我通过一些背景介绍了我们进行检查的最新情况.如果您请求的Sitecore项目不存在,则会得到一个空值,因此很容易处理.但是,如果您请求该特定语言不存在的Sitecore项目,则会返回没有版本的项目.这意味着我们必须进行此检查,因为否则Glass最终将返回空类,我认为这没有多大意义.

这个答案将进行一些实验.

首先需要在Spherical.cs文件中禁用检查:

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

然后,我们可以将支票移到以后的对象构造"管道中.首先创建一个任务:

 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;
            }
        }
    }
}
 

然后最后在GlassMapperScCustom类中注册此任务:

     public static void CastleConfig(IWindsorContainer container){
        var config = new Config();

        container.Register(
            Component.For<IObjectConstructionTask>().ImplementedBy<FallbackCheckTask>().LifestyleTransient()
            );
        container.Install(new SitecoreInstaller(config));
    }
 

我还没有测试过,但是从理论上讲应该可以工作<-免责声明;-)

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 up so that if a user picks a non-default language, they will see the item of that language if it exists. If not, they will see the default ("fallback") language version. We have also set this up at the field level, so that if there is a non-default version of an item but not all the fields are changed, any unchanged fields will fall back to the default language version's value for that field.

Is there anything we can do to enable Glass to use language fallback?

解决方案

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 ;-)

这篇关于Glass.Mapper V3是否可以支持语言回退(字段级别和项目级别)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆