diff --git a/src/Umbraco.Core/Models/ContentExtensions.cs b/src/Umbraco.Core/Models/ContentExtensions.cs index 35fc7382ad..2fd23dabe6 100644 --- a/src/Umbraco.Core/Models/ContentExtensions.cs +++ b/src/Umbraco.Core/Models/ContentExtensions.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; @@ -18,6 +19,46 @@ namespace Umbraco.Core.Models { public static class ContentExtensions { + /// + /// Returns a list of the current contents ancestors, not including the content itself. + /// + /// Current content + /// An enumerable list of objects + public static IEnumerable Ancestors(this IContent content) + { + return ApplicationContext.Current.Services.ContentService.GetAncestors(content); + } + + /// + /// Returns a list of the current contents children. + /// + /// Current content + /// An enumerable list of objects + public static IEnumerable Children(this IContent content) + { + return ApplicationContext.Current.Services.ContentService.GetChildren(content.Id); + } + + /// + /// Returns a list of the current contents descendants, not including the content itself. + /// + /// Current content + /// An enumerable list of objects + public static IEnumerable Descendants(this IContent content) + { + return ApplicationContext.Current.Services.ContentService.GetDescendants(content); + } + + /// + /// Returns the parent of the current content. + /// + /// Current content + /// An object + public static IContent Parent(this IContent content) + { + return ApplicationContext.Current.Services.ContentService.GetById(content.ParentId); + } + /// /// Set property values by alias with an annonymous object /// diff --git a/src/Umbraco.Core/Services/ContentService.cs b/src/Umbraco.Core/Services/ContentService.cs index ec1d53867e..5b5845aa90 100644 --- a/src/Umbraco.Core/Services/ContentService.cs +++ b/src/Umbraco.Core/Services/ContentService.cs @@ -228,7 +228,32 @@ namespace Umbraco.Core.Services } } - /// + /// + /// Gets a collection of objects, which are ancestors of the current content. + /// + /// Id of the to retrieve ancestors for + /// An Enumerable list of objects + public IEnumerable GetAncestors(int id) + { + var content = GetById(id); + return GetAncestors(content); + } + + /// + /// Gets a collection of objects, which are ancestors of the current content. + /// + /// to retrieve ancestors for + /// An Enumerable list of objects + public IEnumerable GetAncestors(IContent content) + { + var ids = content.Path.Split(',').Where(x => x != "-1" && x != content.Id.ToString()).Select(int.Parse).ToArray(); + using (var repository = _repositoryFactory.CreateContentRepository(_uowProvider.GetUnitOfWork())) + { + return repository.GetAll(ids); + } + } + + /// /// Gets a collection of objects by Parent Id /// /// Id of the Parent to retrieve Children from @@ -289,6 +314,27 @@ namespace Umbraco.Core.Services } /// + /// Gets the parent of the current content as an item. + /// + /// Id of the to retrieve the parent from + /// Parent object + public IContent GetParent(int id) + { + var content = GetById(id); + return GetParent(content); + } + + /// + /// Gets the parent of the current content as an item. + /// + /// to retrieve the parent from + /// Parent object + public IContent GetParent(IContent content) + { + return GetById(content.ParentId); + } + + /// /// Gets the published version of an item /// /// Id of the to retrieve version from diff --git a/src/Umbraco.Core/Services/IContentService.cs b/src/Umbraco.Core/Services/IContentService.cs index 2d6871c1b3..ee6960f6af 100644 --- a/src/Umbraco.Core/Services/IContentService.cs +++ b/src/Umbraco.Core/Services/IContentService.cs @@ -287,5 +287,33 @@ namespace Umbraco.Core.Services /// to check if anscestors are published /// True if the Content can be published, otherwise False bool IsPublishable(IContent content); + + /// + /// Gets a collection of objects, which are ancestors of the current content. + /// + /// Id of the to retrieve ancestors for + /// An Enumerable list of objects + IEnumerable GetAncestors(int id); + + /// + /// Gets a collection of objects, which are ancestors of the current content. + /// + /// to retrieve ancestors for + /// An Enumerable list of objects + IEnumerable GetAncestors(IContent content); + + /// + /// Gets the parent of the current content as an item. + /// + /// Id of the to retrieve the parent from + /// Parent object + IContent GetParent(int id); + + /// + /// Gets the parent of the current content as an item. + /// + /// to retrieve the parent from + /// Parent object + IContent GetParent(IContent content); } } \ No newline at end of file