Last active
April 17, 2023 20:13
-
-
Save DanDiplo/e1d79e5dcc525e874ff4 to your computer and use it in GitHub Desktop.
A few useful extension methods for Umbraco IPublishedContent.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using Umbraco.Core; | |
using Umbraco.Core.Models; | |
using Umbraco.Web; | |
/// <summary> | |
/// A few useful extension methods for Umbraco IpublishedContent | |
/// </summary> | |
public static class UmbracoExtensions | |
{ | |
/// <summary> | |
/// Aliases of doctypes that should be excluded from navigation etc. | |
/// </summary> | |
private readonly static string[] ExcludedAliases = new string[] | |
{ | |
"ExampleAlias", "AnotherExampleAlias" | |
}; | |
/// <summary> | |
/// Gets the home page relative to the current content | |
/// </summary> | |
/// <param name="content">The current content</param> | |
/// <returns>The root node of the site</returns> | |
public static IPublishedContent HomePage(this IPublishedContent content) | |
{ | |
return content.AncestorOrSelf(1); | |
} | |
/// <summary> | |
/// Gets whether this content has a template associated with it | |
/// </summary> | |
/// <param name="content">The content to check</param> | |
/// <returns>Returns true if it has a template otherwise false</returns> | |
public static bool HasTemplate(this IPublishedContent content) | |
{ | |
return content.TemplateId > 0; | |
} | |
/// <summary> | |
/// Gets whether this document should be excluded based on it's DocumentTypeAlias | |
/// </summary> | |
/// <param name="content">The content to check</param> | |
/// <returns>True if it should be excluded otherwise false</returns> | |
public static bool IsExcluded(this IPublishedContent content) | |
{ | |
return ExcludedAliases.Contains(content.DocumentTypeAlias); | |
} | |
/// <summary> | |
/// Gets whether this document should appear in the menu | |
/// </summary> | |
/// <param name="content">The content to check</param> | |
/// <returns>True if it should otherwise false</returns> | |
public static bool IsInMenu(this IPublishedContent content) | |
{ | |
return content.HasTemplate() && content.IsVisible() && !content.IsExcluded(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment