Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
namespace Helix.Skeleton.Foundation.Pages.Validators.Base
{
using System;
using Sitecore.Data;
using Sitecore.Data.Items;
using Sitecore.Data.Validators;
public class BaseTemplateDependentValidator : IBaseTemplateDependentValidator
{
private const string TemplateIdToFindParameterName = "TemplateIdToFind";
public ValidatorResult Evaluate<T>(
T baseValidator,
Func<ValidatorResult> evaluate,
Func<Item> getItem)
where T : StandardValidator
{
var templateIdParameter = baseValidator.Parameters[TemplateIdToFindParameterName];
if (string.IsNullOrWhiteSpace(templateIdParameter))
{
return evaluate();
}
var templateIdToFind = ID.Parse(templateIdParameter);
var contextItem = getItem();
if (contextItem.TemplateID == templateIdToFind
|| this.CheckBaseTemplatesRecursively(contextItem.Template, templateIdToFind))
{
return evaluate();
}
return ValidatorResult.Valid;
}
private bool CheckBaseTemplatesRecursively(
TemplateItem templateItem,
ID templateIdToFind)
{
if (templateItem == null || templateItem.BaseTemplates == null)
{
return false;
}
foreach (var baseTemplate in templateItem.BaseTemplates)
{
if (baseTemplate.ID == templateIdToFind
|| this.CheckBaseTemplatesRecursively(baseTemplate, templateIdToFind))
{
return true;
}
}
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment