episerver

Episerver - Consolidate Parent Page Folder/Media Folder as 1 Content Reference Property


I have an existing Content Reference Page property that points to parent page within episerver site.

[CultureSpecific]
        [Display(
            Name = "Parent Page",
            Description = "Select a parent page to display links to all of its subpages.",
            GroupName = SystemTabNames.Content,
            Order = 10)]
        public virtual ContentReference ParentPage { get; set; }


 @if (Model.ParentPage != null)
        {
            @Html.MenuList(Model.ParentPage,
                        @<text>
                            @foreach (var menuItem in item)
                                         {
                                <li class="link-list__item">
                                    <a class="link-list__link" href="@Url.ContentUrl(menuItem.Page.ContentLink)" target="@menuItem.Target">@menuItem.Page.Name</a>
                                </li>
                                         }
                        </text>)
        }


Helper class

   public static IHtmlString MenuList(this HtmlHelper helper, ContentReference rootLink, Func<List<MenuItem>, IHtmlString> itemTemplate, bool requireVisibleInMenu = true, bool requirePageTemplate = true, bool needsThirdLevel = false)
        {
            rootLink = rootLink ?? ContentReference.StartPage;

            var currentContentLink = helper.ViewContext.RequestContext.GetContentLink();

            var pagePath = _contentLoader.Service.GetAncestors(currentContentLink)
                .Reverse()
                .Select(x => x.ContentLink)
                .SkipWhile(x => !x.CompareToIgnoreWorkID(rootLink))
                .ToList();

            var menuItems = _contentLoader.Service.GetChildren<PageData>(rootLink)
                .FilterForDisplay(requirePageTemplate, requireVisibleInMenu)
                .Select((model, index) => CreateMenuItem(model, currentContentLink, pagePath, requirePageTemplate, requireVisibleInMenu, needsThirdLevel, index))
                .ToList();

            return itemTemplate(menuItems);
        }

As an enhancement, I want to modify this property to not just handle Page as it is right now but also Media files. .

PS: I know the straight forward approach is to go with a totally different property with UIHint set to Media Folder. However, on a high level if we look at it, both the property is going to point to a folder (page/media).Its just the way it is going to select the folder for content/media differs & hence looking for a more generic way to handle this.

Any input is appreciated.


Solution

  • The name and description of your ParentPage property would be misleading, but since it's already a ContentReference property it can handle any IContent type.

    You could add an AllowedTypes attribute to explicitly specify which types of content can be set.

    The user can then use the content picker (or drag-and-drop) to set the property to either a page or media.

    Unless it's supposed to be able to handle any type of link, in which case you could consider changing to a LinkItem property.