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

We just updated our project to use Glass.Mapper V3. We love that. But we ran into a problem. He does not seem to respect the language stock.

We have set up our site, so if the user selects a non-standard language, he will see an element of this language, if it exists. If not, they will see the default version ("fallback") language. We also set this at the field level, so if there is a non-standard version of the element, but not all fields are changed, any immutable fields will return to the default version value for this field.

Is there anything we can do to let Glass use the language stock?

+6
source share
2 answers

I am updating this with some information on why we are checking. If you request a Sitecore element that does not exist, you get a null value, so it is easy to handle. However, if you request a Sitecore element that does not exist in that particular language, a versionless element is returned. This means that we must do this check, because otherwise Glass will return an empty class, which, I think, does not make much sense.

This answer will be a bit experimental.

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

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

Then we can transfer the check at a later date to the assembly line of the facility. 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 have not tested this, but should theoretically work <- disclaimer; -)

+6
source

There are several potential problems with the solution provided when using sitecore 7 (7.2) + IoC + solr + mvc.

When using IoC ex Winsdor, make sure your Global.asax looks like <%@ Application Codebehind="Global.asax.cs" Inherits="Sitecore.ContentSearch.SolrProvider.CastleWindsorIntegration.WindsorApplication" Language="C#" %> . Once, by mistake, this file was changed to <%@ Application Codebehind="Global.asax.cs" Inherits="Merck.Manuals.Web.Global" Language="C#" %> , and the language reserve did not work. Also, the errors we received were not descriptive, as we thought the solr schema was incorrect.

Code Sitecore.Context.Items["Disable"] = new VersionCountDisabler(); can be added to PreprocessRequestProcessor, and it works great, which is the best solution to modify global.asax.

+1
source

Source: https://habr.com/ru/post/957073/


All Articles