/
EmptyPageTemplateFilter.cs
31 lines (29 loc) · 1.28 KB
/
EmptyPageTemplateFilter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
using Kentico.PageBuilder.Web.Mvc.PageTemplates;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Controllers.PageTemplates
{
/// <summary>
/// This is to prevent a template from automatically being assigned. If there is 1 non empty template that is available, this will add the "Empty" template as an option so the user can select.
/// </summary>
public class EmptyPageTemplateFilter : IPageTemplateFilter
{
public IEnumerable<PageTemplateDefinition> Filter(IEnumerable<PageTemplateDefinition> pageTemplates, PageTemplateFilterContext context)
{
// only add empty option if there is 1 non empty template remaining, so user has to choose.
var NonEmptyTemplates = pageTemplates.Where(t => !GetTemplates().Contains(t.Identifier));
if(NonEmptyTemplates.Count() > 0)
{
return pageTemplates;
} else
{
// Remove the empty template as an option
return pageTemplates.Where(t => !GetTemplates().Contains(t.Identifier));
}
}
// Gets all page templates that are allowed for landing pages
public IEnumerable<string> GetTemplates() => new string[] { "Empty.Template" };
}
}