3

I'm currently creating my new website using Zend Framework 1.12.3 and I saw something I hate about Zend Navigation:

When a children route is active (exemple: a blog single post, route blog-post), it doesn't mark the parent page (route blog in this case) as active.

Here's my code: (navigation.xml)

<blog>
    <label>BLOG</label>
    <route>blog</route>
    <title>BLOG_TITLE</title>
    <pages>
        <blog-post>
            <route>blog-post</route>
            <visible>false</visible>
        </blog-article>
        <blog-category>
            <route>blog-category</route>
            <visible>false</visible>
        </blog-category>
    </pages>
</blog>

In clear, I would the page blog to be marked as active if a child route like blog-post is active. It doesn't do that unfortunately without using subpages (which I don't want). So if there's a way to mark active the route name blog and all routes who start with blog (exemple: blog-post, blog-category, blog-author) it would be very useful!

I don't want to use subpage for this because of my menu partial render a empty dropdown (Twitter Bootstrap). See by yourself:

Empty dropdown in Zend

Frederick Marcoux
  • 2,195
  • 1
  • 26
  • 57
  • Why not use ZF2? since it's a new site. – Emery King Apr 11 '13 at 02:20
  • I give my main nav elements just a module/controller and leave the action blank - then each sub item has an action. that's how you get what you want. It won't work with just uri's. You have to use the router. – Emery King Apr 11 '13 at 02:24
  • @MarshallHouse I don't like the ZF2 namespaced classes. – Frederick Marcoux Apr 11 '13 at 02:24
  • I don't know if it supports wildcards in the uri's, but you could try `/blog/*` in the parent. – Emery King Apr 11 '13 at 02:25
  • 1
    namespaces are a tiny learning curve and then you don't want to go back. They are so much neater. – Emery King Apr 11 '13 at 02:26
  • @MarshallHouse I edited my file to reflect my router routes. And it's not the namespaces, a lot of what I was using in ZF1 like `headScript` and `headLink` are not available in ZF2 (or I didn't found how they works yet). – Frederick Marcoux Apr 11 '13 at 02:28
  • yes, they are available still - just trickier accessing depending where you are trying to reach them. It's a lot different as far as the mvc goes in ZF2. But the libraries are a lot the same. As for your config here, you won't be able to do it with specific routes, you need to have the entire hierarchy use the same controller and have the default action be the main parent menu item. Understand? – Emery King Apr 11 '13 at 02:31
  • If you have a good tutorial for ZF2, I would try it! And yes, I understand, but if I use ZF2, I will need to recreate all of my application so I'll wait for a tutorial before continuing my website! – Frederick Marcoux Apr 11 '13 at 02:34
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/27993/discussion-between-frederick-marcoux-and-marshall-house) – Frederick Marcoux Apr 11 '13 at 02:35

2 Answers2

5

I know it's already been answered, but I found different solution, because one posted by Marshall didn't work for me. I guess it's because I am not using action/controller in my menu, but routes instead.

My menu entry looks like this:

array(
    'label' => 'cms-nav_users',
    'access_module' => 'user',
    'resource' => 'user_admin',
    'route' => 'admin_user',
    'pages' => array(
        array(
            'label' => 'cms-nav_companies',
            'access_module' => 'user',
            'route' => 'company_admin',
            'resource' => 'company_admin',
        ),
        array(
            'label' => 'cms-nav_users-list',
            'access_module' => 'company',
            'route' => 'admin_user',
            'resource' => 'user_admin',
        ),
    ),
),

I came up with a solution based on my own menu template (partial). Basically there is a foreach loop which checks if any of subpages is active and if there is one - marks parent as active, it looks like this:

$active = false;

if (!empty($page->pages))
{
    foreach ($page->pages as $subpage) 
    {
        if ($subpage->isActive()) $active = true;
    }
}

$html[] = ($active) ?  'class="active"' : '';

Hope it helps someone :)

UPDATE

Actually I found out that you can use Zend Framework's own isActive() with true parameter, as seen in source code:

Zend_Navigation_Page_Mvc::isActive($recursive = false)

Both ways will work, but this one seams more proper.

wasil
  • 358
  • 2
  • 9
  • Well, in fact, I no longer use zf1, now I'm on ZF2, but, that was exactly what I did to make my subpages working! So, thanks dude to approve this! :) – Frederick Marcoux Jul 04 '13 at 04:56
2

from the documentation:

array(
    'label' => 'Page 2',
    'controller' => 'page2',
    'pages' => array(
        array(
            'label' => 'Page 2.1',
            'action' => 'page2_1',
            'controller' => 'page2',
            'class' => 'special-one',
            'title' => 'This element has a special class',
            'active' => true
        ),
        array(
            'label' => 'Page 2.2',
            'action' => 'page2_2',
            'controller' => 'page2',
            'class' => 'special-two',
            'title' => 'This element has a special class too'
        )
    )
),

As you can see, the parent menu item has no action assigned to it, but the parents and children share the same controller. That's how you can have active states on the parent of a child page.

Emery King
  • 3,550
  • 23
  • 34
  • Join me on the chat http://chat.stackoverflow.com/rooms/27993/discussion-between-frederick-marcoux-and-marshall-house please. I have some questions about ZF2 if you are willing to anwser them better than ZF docs! :) – Frederick Marcoux Apr 11 '13 at 02:42