am-impact / amnav

Navigation plugin for Craft
168 stars 19 forks source link

How do you use getActivePageIdForLevel #35

Closed WhirlyGuy closed 9 years ago

WhirlyGuy commented 9 years ago

Link - https://github.com/am-impact/amnav/issues/5

@hubertprein Just wondering how you go about using that getActivePageIdForLevel function in conjunction with the startFromId parameter?

A bit of a newb to Craft and associated plugins so not too sure how you code this sort of thing? Didn't see it mentioned in the Docs either so a little confused as to exactly how to do that sort of thing as mentioned in the post linked to above.

Many thanks in advance for any help with this.

P.S. Apologies for posting to a closed issue like that.

hubertprein commented 9 years ago

@WhirlyGuy

See the example below:

{% set activeNodeId = craft.amNav.getActiveNodeIdForLevel('yourNavigationHandle') %}

{% if activeNodeId %}
    {{ craft.amNav.getNav('yourNavigationHandle', {
        id: 'subnav',
        class: 'subnav',
        startFromId: activeNodeId
    }) }}
{% endif %}

The getActiveNodeIdForLevel function has a second parameter, where you can insert a segment. By default, it will try to find the active node ID for the first segment. If you want to find it for the 2nd (3rd, 4th, etc..) segment, simply add a 2 (3 or 4, etc..) as the second parameter.

Please let me know if this helps.

WhirlyGuy commented 9 years ago

Hiya,

Many thanks for the reply. I've tried that code that you have given me but not getting anything to output to the page at all. Also if I spit out {{ activeNodeId }} after it has been set then nothing spits out to the page at all so I'm wondering if it's actually getting set in the first place at all?

What I'm really looking for is a way to do something like this : http://craftcms.stackexchange.com/questions/9942/navigation-structure-usage-woes

I'm hoping that I can do that with AM Nav although at the moment I just can't get it to work at all I'm afraid. I'm sure it's something silly that I'm doing here but just can't wrap my head around what :-(

If I place

{% set activeNodeId = craft.amNav.getActiveNodeIdForLevel('navigation', '3') %}
Active Node Id = {{ activeNodeId }}<hr />
{% if activeNodeId %}
    {{ craft.amNav.getNav('navigation', {
        id: 'subnav',
        class: 'subnav',
        startFromId: activeNodeId
    }) }}
{% endif %}

on the standard news/_entry.html template that comes with a fresh Craft install then nothing gets spat out at all. Really sorry to bug you like this but I'm positive this has to be something stupid that I'm doing somewhere?

Many thanks,

Mark

hubertprein commented 9 years ago

That's because the plugin can't find an active node for that segment. If your navigation doesn't have a third level with elements in it, it won't find an active node.

Perhaps if you explain what you try to achieve here, I could help out.

WhirlyGuy commented 9 years ago

Hiya. Many thanks for taking the time on this one with me. Basically what I'm after is using your plugin so that a client can easily build up their site menu very quickly. This they can do with the plugin with no problems at all.

I would then like to be able to have a secondary navigation within the templates that would just show the items for the section they are in so for example if the full AM Nav is this sort of thing :

Services ---- Service 1 ---- Service 2 -------- Sub Service ---- Service 3 Galleries ---- Gallery 1 ---- Gallery 2 .......

... and so on and so forth, if you visit say Service 2 then on that page a secondary AM Nav tag would output

Services ---- Service 1 ---- Service 2 -------- Sub Service ---- Service 3

Same goes for if you were to visit say Gallery 2 then you would get

Galleries ---- Gallery 1 ---- Gallery 2

This would allow the person navigating the site to see there are more items to see in the current main section that they are visiting and to get to them easily.

Hopefully that made more sense and really hoping that is possible with the plugin? Seems like it will be?

Again, many thanks for any help you can give with this as your plugin is really awesome and your time and help is much appreciated.

WhirlyGuy commented 9 years ago

Hiya,

Was just wondering if this is possible at all with the plugin or should I be looking at some other way of doing this?

Many thanks,

Mark

hubertprein commented 9 years ago

Sorry for the late reply.

What you're trying to do, is possible. As long as you create a navigation that'll contain all the pages you want to see in a submenu later on. With your services example, if you add those in a navigation, you're then able to use the tag on a service's template.

On the service (not the overview) template you'll use the tag, that'll get the nodeId for the services overview. So basically you'll get the sample above:

{% set activeNodeId = craft.amNav.getActiveNodeIdForLevel('yourNavigationHandle') %}

{% if activeNodeId %}
    {{ craft.amNav.getNav('yourNavigationHandle', {
        id: 'subnav',
        class: 'subnav',
        startFromId: activeNodeId
    }) }}
{% endif %}

You could put this code in an separate file, and include it on pages you want to use it on. Simply by using:

{% include '_includes/nav/sub' %}

Or something like that.

hubertprein commented 9 years ago

I recon things worked out. Closing the issue!

bootsified commented 7 years ago

I hate to continue such an old thread, but I'm having the same problem and it doesn't seem like the issue was fully resolved here. Here is the code block I'm using:

{% set activeNodeId = craft.amNav.getActiveNodeIdForLevel('mainNav') %}

<div class="subnav">
    <div class="l-container">
        <strong class="subnav__label">{{ navTitle }} - {{ activeNodeId }}</strong>
        <nav>

            {{ craft.amNav.getNav("mainNav", {
                class: 'subnav__nav',
                classActive: 'is-active',
                maxLevel: 1,
                startFromId: activeNodeId,
            }) }}

        </nav>
    </div>
</div>

...and here is my entries setup:

screen shot 2017-10-02 at mon oct 2 2017 9 47 40 am

...and here is my nav setup:

screen_shot_2017-10-02_at_mon__oct_2__2017___9_47_02_am


From what I can tell, I'm setting things up correctly, but I'm not able to get the activeNodeId - it just returns nothing, so the code renders the top-level nav every time. I've also tried using the second parameter, $segmentLevel, in the getActiveNodeIdForLevel call, but nothing changes no matter what number I put there. There's nothing in the docs about getActiveNodeIdForLevel, so I may be doing it wrong. Any suggestions?