stho01
3/9/2017 - 1:11 PM

A util class that contains functions to filter epi server contents based on access and published state.

A util class that contains functions to filter epi server contents based on access and published state.

public static class ContentExtensions
{
    /// <summary>
    /// Filters content which should not be visible to the user. 
    /// </summary>
    public static IEnumerable<T> FilterForDisplay<T>(this IEnumerable<T> contents, bool requirePageTemplate = false, bool requireVisibleInMenu = false)
        where T : IContent
    {
        var accessFilter = new FilterAccess();
        var publishedFilter = new FilterPublished();
        contents = contents.Where(x => !publishedFilter.ShouldFilter(x) && !accessFilter.ShouldFilter(x));
        if (requirePageTemplate)
        {
            var templateFilter = ServiceLocator.Current.GetInstance<FilterTemplate>();
            templateFilter.TemplateTypeCategories = TemplateTypeCategories.Page;
            contents = contents.Where(x => !templateFilter.ShouldFilter(x));
        }
        if (requireVisibleInMenu)
        {
            contents = contents.Where(x => VisibleInMenu(x));
        }
        return contents;
    }

    /// <summary>
    /// A visible in menu check that has a null check in addition. 
    /// </summary>
    /// <param name="content"></param>
    /// <returns></returns>
    private static bool VisibleInMenu(IContent content)
    {
        var page = content as PageData;
        if (page == null)
        {
            return true;
        }
        return page.VisibleInMenu;
    }
}