public static IEnumerable <TContent> GetDescendents <TContent>(this ContentReference contentRef, Expression <Func <TContent, bool> > filterExpression = null, LanguageSelector languageSelector = null) where TContent : class, IContent { if (ContentReference.IsNullOrEmpty(contentRef)) { yield break; } var filterFunc = filterExpression == null ? null : filterExpression.Compile(); if (languageSelector == null) { languageSelector = new NullLanguageSelector(); } foreach (var child in contentRef.GetChildren <IContent>(null, languageSelector)) { if (child is TContent && (filterFunc == null || filterFunc(child as TContent))) { yield return(child as TContent); } foreach (var grandChild in child.ContentLink.GetDescendents <TContent>(filterExpression, languageSelector)) { yield return(grandChild); } } }
/// <summary> /// Loads a <see cref="IContent"/> instance from a <see cref="ContentReference"/> optionally from a certain language and filtered for visitor access /// </summary> /// <typeparam name="TContentType">Type of content to load</typeparam> /// <param name="contentLink"><see cref="ContentReference"/> of the content to load</param> /// <param name="languageSelector">The <see cref="ILanguageSelector"/> to use.</param> /// <param name="filterForVisitor">If the content should be filtered by visitor access rights.</param> /// <param name="contentLoader">Optional <see cref="IContentLoader"/>, uses <see cref="ApplicationServices.CurrentServiceLocator"/> to resolve if null</param> /// <returns>A <see cref="IContent"/> instance, or null if not found</returns> public static TContentType Get <TContentType>(this ContentReference contentLink, bool filterForVisitor = false, IContentLoader contentLoader = null, LanguageSelector languageSelector = null) where TContentType : class, IContent { if (ContentReference.IsNullOrEmpty(contentLink)) { return(null); } if (contentLoader == null) { contentLoader = ServiceLocator.Current.GetInstance <IContentLoader>(); } if (languageSelector == null) { languageSelector = new NullLanguageSelector(); } //Logger.Default.Log(string.Format("Get: getting content of type '{3}' for reference '{0}' with language selector {1} using content loader '{2}'", contentLink, languageSelector, contentLoader.GetType().FullName, typeof(TContentType).FullName), LogType.Debug); try { var content = contentLoader.Get <TContentType>(contentLink, languageSelector); return(filterForVisitor ? new[] { content }.FilterForVisitor().FirstOrDefault() : content); } catch (ContentNotFoundException) { } catch (TypeMismatchException) { } return(null); }
/// <summary> /// gets all children of a certain type /// </summary> /// <typeparam name="TContent"></typeparam> /// <param name="contentRef"></param> /// <param name="filterExpression">A Lambda to filter out the pages that do not comply</param> /// <returns></returns> public static IEnumerable <TContent> GetChildren <TContent>(this ContentReference contentRef, Expression <Func <TContent, bool> > filterExpression = null, LanguageSelector languageSelector = null) where TContent : class, IContent { if (ContentReference.IsNullOrEmpty(contentRef)) { return(null); } IContentRepository contentRepo = ServiceLocator.Current.GetInstance <IContentRepository>(); if (languageSelector == null) { languageSelector = new NullLanguageSelector(); } if (filterExpression == null) { return(contentRepo.GetChildren <TContent>(contentRef, languageSelector).FilterForVisitor()); } var filterFunc = filterExpression.Compile(); return(contentRepo.GetChildren <TContent>(contentRef, languageSelector).FilterForVisitor().Where(page => filterFunc(page))); }