Sitecore 8.2 Language Fallback – Glass Mapper

If you are using Glass Mapper v4.4.0.199 and Sitecore 8.2 with Language Fallback enabled you will notice that SitecoreContext.GetItem, .GetRootItem, etc. will return null if the current item language version doesn’t exist.

ItemNull

By default, Glass Mapper checks to see if a Sitecore item has a version in the language of the item you requested. If you have fallback enabled this check will return null. Here is a solution how to avoid this issue adding a version count disabler and it will return your item fallback item. If you noticed, it uses the Global.asax to handle requests and it’s no longer supported in Sitecore 8.2.

To make it work you have to create two processors, one will be executed when the request begins and the second once the requested is being terminated:

public class VersionCountDisablerBegin : HttpRequestProcessor
{
	public override void Process(HttpRequestArgs args)
	{
		Assert.ArgumentNotNull(args, "args");

		HttpContext.Current.Items["VersionCountDisabler"] = new VersionCountDisabler();
	}
}
public class VersionCountDisablerEnd : HttpRequestProcessor
{
	public override void Process(HttpRequestArgs args)
	{
		Assert.ArgumentNotNull(args, "args");

		var disabler = HttpContext.Current.Items["VersionCountDisabler"] as VersionCountDisabler;
		HttpContext.Current.Items["VersionCountDisabler"] = null;

		disabler?.Dispose();
	}
}

 
…and reference them in a config file:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
      <httpRequestBegin>
        <processor type="Sitecore.Foundation.SitecoreProcessors.VersionCountDisablerBegin, Sitecore.Foundation.SitecoreProcessors"
                   patch:before="*[1]" />
      </httpRequestBegin>

      <httpRequestEnd>
        <processor type="Sitecore.Foundation.SitecoreProcessors.VersionCountDisablerEnd, Sitecore.Foundation.SitecoreProcessors" />
      </httpRequestEnd>
    </pipelines>
  </sitecore>
</configuration>

 

Finally, you will see that item is not null since it will fallback to the one you have set.

ItemNotNull

 
Hope this helps!
Happy Sitecoring 😉
 

 

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s