Copied to clipboard

Flag this post as spam?

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


  • Johan 95 posts 264 karma points
    Dec 04, 2015 @ 14:09
    Johan
    0

    Hide navigation menus for non-logged in users

    I would like to hide some the menus on the navigation that a member has not access to.

    For example if member1 has access to has access to everything then show every menu on the navigation. But if member2 doesn't have access to meny2, -hide it from the navigation

    How can I do this?

    Here is my navigation macro code.

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @{
        int num = 1;    
        var rootNode = CurrentPage.Site();
    
        <ul id="menu">
    
            <li class="thehome">
                <a href="@rootNode.Url" class="drop home"></a>
            </li>
    
    
            @foreach (var mainNode in rootNode.Children().Where("Visible"))
            {
                int childCount = 1;
                int numChildren = mainNode.Children().Count();
    
                <li>
                    <a href="@mainNode.Url" class="drop">@mainNode.Name</a>
    
                    @{
                        var childNodes = mainNode.Children().Where("Visible");
                    }
    
                    @if (childNodes.Any())
                    {
                        <div id=nav-@num class="dropdown_5columns">
    
                            <!-- Begin 2 columns container -->
                            <div class="col_5">
                                <h2>@mainNode.Name</h2>
                            </div>
    
                            @* note if you want ALL descendants change .Children to .Descendats*@
                            @foreach (var childNode in childNodes)
                            {
                                // if first node or new set of three open the div and ul @: is used to stop razor trying to
                                // "balance" the tags
                                if (childCount == 1 || (double)childCount % 3 == 1)
                                {
                                    @:<div class="col-md-4">
                                    @:<ul class="subnav">
                                }
                                <li><a href="@childNode.Url">@childNode.Name</a></li>
    
                                // close the div and list if this is either a multiple of 3 or the last one
                                if ((double)childCount % 3 == 0 || numChildren == childCount)
                                {
                                    @:</ul>
                                    @:</div>
                                }
    
                                childCount++;
    
                            }
    
                        </div>
                    }
    
                </li>
                num++;
            }
        </ul>
    }
    
  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Dec 04, 2015 @ 14:52
    Dave Woestenborghs
    0

    Hi Johan,

    You will need to use the MemberHasAccess and IsProtected methods of the UmbracoHelper.

    You can find the documentation over here : https://our.umbraco.org/Documentation/Reference/Querying/UmbracoHelper/

  • Johan 95 posts 264 karma points
    Dec 04, 2015 @ 15:22
    Johan
    0

    Hi Dave, How would the code look like inside my code?

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Dec 07, 2015 @ 08:24
    Dave Woestenborghs
    0

    Hi Johan,

    Here you can find a code example that "should" work. First thing I did is change from the dynamics syntax to strongly typed.

    See this article for more information about tht difference : http://24days.in/umbraco/2015/strongly-typed-vs-dynamic-content-access/

    And here is the code example

    @using Umbraco.Web;
    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @{
        int num = 1;    
        var rootNode = Model.Content.AncestorOrSelf("Site");
    
        <ul id="menu">
    
            <li class="thehome">
                <a href="@rootNode.Url" class="drop home"></a>
            </li>
    
    
            @foreach (var mainNode in rootNode.Children().Where(x => x.IsVisible() 
                && (!Umbraco.IsProtected(x.Id, x.Path) || Umbraco.MemberHasAccess(x.Id, x.Path))))
            {
                int childCount = 1;
                int numChildren = mainNode.Children().Count();
    
                <li>
                    <a href="@mainNode.Url" class="drop">@mainNode.Name</a>
    
                    @{
                        var childNodes = mainNode.Children().Where(x => x.IsVisible() 
                        && (!Umbraco.IsProtected(x.Id, x.Path) || Umbraco.MemberHasAccess(x.Id, x.Path)));
                    }
    
                    @if (childNodes.Any())
                    {
                        <div id=nav-@num class="dropdown_5columns">
    
                            <!-- Begin 2 columns container -->
                            <div class="col_5">
                                <h2>@mainNode.Name</h2>
                            </div>
    
                            @* note if you want ALL descendants change .Children to .Descendats*@
                            @foreach (var childNode in childNodes)
                            {
                                // if first node or new set of three open the div and ul @: is used to stop razor trying to
                                // "balance" the tags
                                if (childCount == 1 || (double)childCount % 3 == 1)
                                {
                                    @:<div class="col-md-4">
                                    @:<ul class="subnav">
                                }
                                <li><a href="@childNode.Url">@childNode.Name</a></li>
    
                                // close the div and list if this is either a multiple of 3 or the last one
                                if ((double)childCount % 3 == 0 || numChildren == childCount)
                                {
                                    @:</ul>
                                    @:</div>
                                }
    
                                childCount++;
    
                            }
    
                        </div>
                    }
    
                </li>
                num++;
            }
        </ul>
    }
    

    Dave

  • Johan 95 posts 264 karma points
    Dec 07, 2015 @ 08:48
    Johan
    0

    I get error

    Error loading Partial View script (file: ~/Views/MacroPartials/MainNavigation.cshtml)

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Dec 07, 2015 @ 09:24
    Dave Woestenborghs
    0

    Maybe you change this line :

    var rootNode = Model.Content.AncestorOrSelf(1);
    

    I was assuming that your top node would be of the documenttype "Site".

    Dave

  • Johan 95 posts 264 karma points
    Dec 07, 2015 @ 10:11
    Johan
    0

    Hmm I still get the same error for some reason.

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Dec 07, 2015 @ 10:40
    Dave Woestenborghs
    0

    Can you show me how you call this macro from your template ?

    Because it works on my test site.

    Dave

  • Johan 95 posts 264 karma points
    Dec 07, 2015 @ 10:52
    Johan
    0

    This is my macro:

    @using Umbraco.Web;
    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @{
        int num = 1;
        var rootNode = Model.Content.AncestorOrSelf(1);
    
        <ul id="menu">
    
            <li class="thehome">
                <a href="@rootNode.Url" class="drop home"></a>
            </li>
    
    
            @foreach (var mainNode in rootNode.Children().Where(x => x.IsVisible()
                && (!Umbraco.IsProtected(x.Id, x.Path) || Umbraco.MemberHasAccess(x.Id, x.Path))))
            {
                int childCount = 1;
                int numChildren = mainNode.Children().Count();
    
                <li>
                    <a href="@mainNode.Url" class="drop">@mainNode.Name</a>
    
                    @{
                var childNodes = mainNode.Children().Where(x => x.IsVisible()
                && (!Umbraco.IsProtected(x.Id, x.Path) || Umbraco.MemberHasAccess(x.Id, x.Path)));
                    }
    
                    @if (childNodes.Any())
                    {
                        <div id=nav-@num class="dropdown_5columns">
    
                            <!-- Begin 2 columns container -->
                            <div class="col_5">
                                <h2>@mainNode.Name</h2>
                            </div>
    
                            @* note if you want ALL descendants change .Children to .Descendats*@
                            @foreach (var childNode in childNodes)
                            {
                                // if first node or new set of three open the div and ul @: is used to stop razor trying to
                                // "balance" the tags
                                if (childCount == 1 || (double)childCount % 3 == 1)
                                {
                                    @:<div class="col-md-4">
                                        @:<ul class="subnav">
                                }
                                <li><a href="@childNode.Url">@childNode.Name</a></li>
    
                                // close the div and list if this is either a multiple of 3 or the last one
                                if ((double)childCount % 3 == 0 || numChildren == childCount)
                                {
                                    @:
                                </ul>
                                @:
                            </div>
                                }
    
                                childCount++;
    
                            }
    
                        </div>
                    }
    
                </li>
                    num++;
            }
        </ul>
    }
    

    And I call it from this this template:

    inherits UmbracoTemplatePage
    @using Newtonsoft.Json.Linq
    @{
        var root = CurrentPage.Site();
        var home = Umbraco.Content(root.rootpage);
    }
    
    <header>
    
        <div class="row">
            <nav class="entry-links">
                <ul>
    
                    @{
                        if (home.HasValue("topLinks"))
                        {
                            foreach (var item in home.GetPropertyValue<JArray>("topLinks"))
                            {
                                if (item.Value<bool>("isInternal"))
                                {
                                    var nodeId = item.Value<int>("internal");
                                    var node = Umbraco.Content(nodeId);
                                    var linkUrl = (item.Value<bool>("isInternal")) ? Umbraco.NiceUrl(item.Value<int>("internal")) : item.Value<string>("link");
                                    <li id="elever" class="toplink @(node.IsAncestorOrSelf(CurrentPage) ? "selected" : null)">
                                        <a href="@linkUrl" title="@node.Name">@node.Name</a>
                                    </li>
                                }
                            }
                        }
                    }
    
                </ul>
            </nav>
        </div>
    
        <div class="container">
    
            <div class="row logo">
                <div class="col-xs-8 col-sm-12 col-md-4">
                    <a href="@home.Url">
                        <img alt="Brand" src="@Umbraco.Media(home.siteLogo).Url">
                    </a>
                </div>
            </div>
    
            <div class="row">
                <div class="col-md-12 main-nav">
                    <nav id="mainNav">
                        @Umbraco.RenderMacro("MainNavigation") @*<---------------- The main navigation*@
                    </nav>
                    <nav class="nav-mobile">
                        @Umbraco.RenderMacro("MobileNav")
                        </nav>
               </div>
                <br />
                <br />
            </div>
    
            <div class="container-fluid">
            </div>
        </div>
    
    </header>
    
  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Dec 07, 2015 @ 11:00
    Dave Woestenborghs
    0

    Hi Johan

    Without more details about the error I would do the following to try to reproduce this.

    Remove the call to the main navigation macro from the template. If you still get the error than you now the error is somewhere in your template.

    If you don't get a error you can add the macro a back.

    Than I would try to strip the macro from all it's code and would put back little pieces of code till I have located the error.

    Dave

  • Johan 95 posts 264 karma points
    Dec 07, 2015 @ 13:27
    Johan
    0

    Hi Dave,

    The problem seem to be this:

     @foreach (var childNode in childNodes)
    {
    
                // if first node or new set of three open the div and ul @: is used to stop razor trying to
                // "balance" the tags
                if (childCount == 1 || (double)childCount % 3 == 1)
                {
                    @:<div class="col-md-4">
                        @:<ul class="subnav">
                }
                <li><a href="@childNode.Url">@childNode.Name</a></li>
    
                // close the div and list if this is either a multiple of 3 or the last one
                if ((double)childCount % 3 == 0 || numChildren == childCount)
                {
                    @:
                </ul>
                @:
            </div>
                }
    
                childCount++;
    
    
    
    }
    

    Do you have any idea why it's an error?

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Dec 07, 2015 @ 13:34
    Dave Woestenborghs
    100

    It seems you want to render some tags after every third item. You can do this more easy using this syntax :

     @foreach (var group in childNodes.InGroupsOf(3))
                            {
                                <div class="col-md-4">
                                    <ul class="subnav">
    
                                        @foreach(var childNode in group){
                                            <li><a href="@childNode.Url">@childNode.Name</a></li>
                                        }
                                    </ul>
                                </div>
                            }
    

    Dave

  • Dave Woestenborghs 3504 posts 12135 karma points MVP 9x admin c-trib
    Dec 07, 2015 @ 13:29
    Dave Woestenborghs
    0

    I see you do some calculations on the count. Maybe there is a error in that ?

    If you remove this.. Does it work ?

    Dave

  • Johan 95 posts 264 karma points
    Dec 07, 2015 @ 13:46
    Johan
    0

    Thank you so much Dave! It's working now.

Please Sign in or register to post replies

Write your reply to:

Draft