Created
January 19, 2016 18:18
-
-
Save mskutta/c2b5eec17c3821d183a8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ResolveItemTask : IConfigurationResolverTask | |
{ | |
public void Execute(ConfigurationResolverArgs args) | |
{ | |
// Don't impact out of the box functionality for the page editor. | |
if (Sitecore.Context.PageMode.IsPageEditorEditing) | |
return; | |
var context = (SitecoreTypeCreationContext)args.AbstractTypeCreationContext; | |
// Item does not exist or language version already exists, bail out. | |
if (context.Item == null || context.Item.Versions.Count > 0) | |
return; | |
var languageFallbackState = Switcher<LanguageItemFallbackState>.CurrentValue; | |
if (languageFallbackState == null || languageFallbackState.Languages == null) | |
return; | |
var service = (SitecoreService)args.Service; | |
var id = context.Item.ID; | |
foreach (var language in languageFallbackState.Languages) | |
{ | |
var item = service.Database.GetItem(id, language); | |
// Bail out if no item. In this case item should be null regardless of language. (you will always get a language version back regardless of translations) | |
if (item == null) | |
break; | |
// No language version exists. Check the next language | |
if (item.Versions.Count == 0) | |
continue; | |
context.Item = item; | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment