Skip to content

Instantly share code, notes, and snippets.

@jraps20
Created March 30, 2018 01:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jraps20/835b554190a545fa23822667b11b2f56 to your computer and use it in GitHub Desktop.
Save jraps20/835b554190a545fa23822667b11b2f56 to your computer and use it in GitHub Desktop.
[Validator("Language Fallback Invalid", Status.Error, Description = "Validates all fields support language fallback. Add new properties to ignore templates that start with a particular path.")]
public class LanguageFallbackValidator : UserConfigurableValidator
{
private static readonly Guid Template = new Guid("{AB86861A-6030-46C5-B394-E8F99E8B87DB}");
public override IEnumerable<Problem> Validate(Dictionary<Guid, SitecoreDeployInfo> projectItems, XDocument scprojDocument)
{
var list = new List<Problem>();
foreach (var projectItem in projectItems)
{
if (Settings.Properties.Where(i => !string.IsNullOrWhiteSpace(i)).Any(i => projectItem.Value.Item.SitecoreItemPath.StartsWith(i, StringComparison.InvariantCultureIgnoreCase)))
continue;
var str = projectItem.Value.ParsedItem.Properties["template"];
if (string.IsNullOrEmpty(str))
continue;
var guid = new Guid(str);
if (guid == Template)
{
list.AddRange(ValidateTemplate(projectItems, projectItem.Value.Item));
}
}
list.RemoveAll(r => r == null);
return list;
}
private IEnumerable<Problem> ValidateTemplate(Dictionary<Guid, SitecoreDeployInfo> projectItems, SitecoreItem sitecoreItem)
{
if (!sitecoreItem.Children.Any())
{
var noChild = new Problem(this, $"{sitecoreItem.SitecoreItemPath} has no children.");
yield return noChild;
yield break;
}
var standardValue = sitecoreItem.Children.Values.FirstOrDefault(i => projectItems[i.ItemId].ParsedItem.Name == "__Standard Values");
if (standardValue == null)
{
var noStandardValues = new Problem(this, $"{sitecoreItem.SitecoreItemPath} has no '__Standard Values' item.");
yield return noStandardValues;
yield break;
}
var standardValueItem = projectItems[standardValue.ItemId];
var languageFallback = standardValueItem.ParsedItem.Fields["__Enable item fallback"];
if (languageFallback != null && languageFallback == "1")
yield break;
var noLanguageFallback = new Problem(this, $"{sitecoreItem.SitecoreItemPath} does not have 'Enable Item Fallback' checked.");
yield return noLanguageFallback;
}
public override ValidatorSettings GetDefaultSettings()
{
//Return a default list of locations to check
return new ValidatorSettings
{
Properties = new List<string> { "" }
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment