Skip to content

Instantly share code, notes, and snippets.

@seangwright
Created March 14, 2021 23:54
Show Gist options
  • Save seangwright/4873c3f72c5c61ad0831237e96ba59cf to your computer and use it in GitHub Desktop.
Save seangwright/4873c3f72c5c61ad0831237e96ba59cf to your computer and use it in GitHub Desktop.
Kentico Xperience Page Template Filter base class
/// <summary>
/// Default filter for associating Page Types to Page Templates
/// </summary>
public abstract class DefaultPageTemplateFilter : IPageTemplateFilter
{
/// <summary>
/// If true, then the templates returned by <see cref="FilterBy(PageTemplateDefinition)"/> are excluded from the results of <see cref="Filter(IEnumerable{PageTemplateDefinition}, PageTemplateFilterContext)"/>
/// when the <see cref="PageTypeClassName"/> does not match the Page Type of the context.
/// Defaults to false.
/// </summary>
public virtual bool ExcludeIfNoMatch { get; } = false;
/// <summary>
/// The prefix of the <see cref="RegisterComponentAttribute.Identifier"/> of the given Page Template on which to match
/// </summary>
public abstract string PageTemplatePrefix { get; }
/// <summary>
/// The <see cref="TreeNode.ClassName"/> used to compare to the <see cref="PageTemplateFilterContext.PageType"/>
/// </summary>
public abstract string PageTypeClassName { get; }
public IEnumerable<PageTemplateDefinition> Filter(IEnumerable<PageTemplateDefinition> pageTemplates, PageTemplateFilterContext context)
{
if (context.PageType.Equals(PageTypeClassName, StringComparison.InvariantCultureIgnoreCase))
{
return pageTemplates.Where(FilterBy);
}
if (ExcludeIfNoMatch)
{
return pageTemplates.Where(t => !FilterBy(t));
}
return pageTemplates;
}
/// <summary>
/// By default this matches if the definition Identifier starts with the <see cref="PageTemplatePrefix"/>
/// </summary>
/// <param name="definition"></param>
/// <returns></returns>
protected virtual bool FilterBy(PageTemplateDefinition definition) =>
definition.Identifier.StartsWith(PageTemplatePrefix, StringComparison.OrdinalIgnoreCase);
}
public class HomePageTemplateFilter : DefaultPageTemplateFilter
{
public override string PageTemplatePrefix => "Sandbox_Home_";
public override string PageTypeClassName => HomePage.CLASS_NAME;
}
public static class ServiceCollectionExtensions
{
/// <summary>
/// Adds all existing Page Template Filters to the <see cref="PageBuilderFilters.PageTemplates"/> via reflection
/// </summary>
/// <param name="services"></param>
/// <param name="assembiles"></param>
/// <returns></returns>
/// <example>
/// services.AddPageTemplateFilters(Assembly.GetExecutingAssembly())
/// </example>
public static IServiceCollection AddPageTemplateFilters(this IServiceCollection services, params Assembly[] assembiles)
{
var types = assembiles.SelectMany(a => a.GetExportedTypes()
.Where(t => !t.IsAbstract && !t.IsInterface && t.IsAssignableTo(typeof(IPageTemplateFilter))));
foreach (var t in types)
{
object? instance = Activator.CreateInstance(t);
if (instance is IPageTemplateFilter filter)
{
PageBuilderFilters.PageTemplates.Add(filter);
}
}
return services;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment