Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Mov3nforward 117 posts 319 karma points
    Jan 07, 2021 @ 02:18
    Mov3nforward
    0

    Getting number of child nodes in Umbraco V8

    I am trying to figure out how to check if a node has child nodes for a menu.

    In v7 I had something like this.

    var mainNavigation = navigation.GetPropertyValue<IEnumerable<Link>>("mainNavigation");
    <div class="navbar-wrap">
        <nav class="navbar navbar-expand-lg">
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbar" aria-controls="navbar" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div id="navbar" class="collapse navbar-collapse">
                <ul class="navbar-nav ml-auto">
                    @foreach (var item in mainNavigation)
                    {           
    
                            int childNodeCnt = 0;
                            childNodeCnt = Umbraco.TypedContent(item.Udi).Children().Where("Visible").Where("hideFromMenu == false").Count();
    

    The code for V7 does not work for v8 and I am not sure how to translate it.

  • Huw Reddick 1929 posts 6697 karma points MVP 2x c-trib
    Jan 07, 2021 @ 08:29
    Huw Reddick
    0

    try something like this

    item.Children().Where(x => x.IsVisible()) && !item.Value<bool>("hideFromNav")).Count()
    
  • Nik 1614 posts 7260 karma points MVP 7x c-trib
    Jan 07, 2021 @ 08:59
    Nik
    101

    Hi Mov3nforward,

    I would do something like this:

    var contentNode = Umbraco.TypedContent(item.Udi);
    int childNodeCnt = contentNode == null ? 0 :
             contentNode.Children().Count(c => c.IsVisible() && !c.Value<bool>("hideFromMenu"));
    

    This takes advantage of full Linq statements, where as your v7 example is using dynamic linq which is no longer present in v8. It also adds in a check to make sure you get a content item back from the item.Udi line.

    Hope that helps.

    Nik

  • Mov3nforward 117 posts 319 karma points
    Jan 07, 2021 @ 21:56
    Mov3nforward
    0

    @Nik,

    Your code throws an error.

    CS1061: 'UmbracoHelper' does not contain a definition for 'TypedContent' and no accessible extension method 'TypedContent' accepting a first argument of type 'UmbracoHelper' could be found (are you missing a using directive or an assembly reference?)

  • Nik 1614 posts 7260 karma points MVP 7x c-trib
    Jan 07, 2021 @ 22:03
    Nik
    0

    That would be because I was an idiot and forgot to update that line to be v8 compatible. Sorry about that. It should read:

    var contentNode = Umbraco.Content(item.Udi);
    

    Sorry about that. I'm also making the assumption that item.Udi is giving you the correct value that is expected by that method. I can't recall if it takes a UDI, or if you need to get the id/guid from it first.

    Nik

  • Mov3nforward 117 posts 319 karma points
    Jan 07, 2021 @ 22:26
    Mov3nforward
    0

    Thank you that works.

    Now I am trying to pass to a helper the parent node so I can get the subpages to populate a dropdown menu.

    var contentNode = Umbraco.Content(link.Udi);
                            int childNodeCnt = contentNode == null ? 0 : contentNode.Children().Count(c => c.IsVisible() && !c.Value<bool>("hideFromMenu"));
    
    
    
                                <li class="menu-item">
                                    <a class="menu-link" href="@linkURL" title="@Html.Raw(linkTitle)" target="@Html.Raw(linkTarget)"><div>@link.Name</div></a>
    
                                    if(childNodeCnt > 0 && contentNode.DocumentTypeAlias != "homePage" && contentNode.DocumentTypeAlias == "parentNode" )
                                    { 
                                        <div class="dropdown-menu">
                                         @GetDropDown(contentNode)
                                        </div>
                                    }
    
                                </li>
    

    /**********

    @helper GetDropDown(IEnumerable<IPublishedContent> pages){
    if(pages.Count() > 0){
        <div class="col">
        @foreach(var node in pages){
            <a class="dropdown-item" href="@node.Url">
                @node.Name
            </a>
        }
        </div>
    }}
    

    I am getting this error. CS1503: Argument 1: cannot convert from 'Umbraco.Core.Models.PublishedContent.IPublishedContent' to 'System.Collections.Generic.IEnumerable

  • Huw Reddick 1929 posts 6697 karma points MVP 2x c-trib
    Apr 22, 2021 @ 21:08
    Huw Reddick
    1

    you probably need to do

    @GetDropDown(contentNode.Children)
    

    you are just passing in a contentnode, but you want to pass in the children instead as your hepler is expecting a collection

Please Sign in or register to post replies

Write your reply to:

Draft