Organizational Research By

Surprising Reserch Topic

zf2 dynamically created sub pages in navigation doesnt become active isactive


zf2 dynamically created sub pages in navigation doesnt become active isactive  using -'zend-framework,zend-framework2'

I have the following issue...

This is my navigation:

return array(
    'navigation' => array(
        'site' => array(
            array(
                'label' => 'Home',
                'route' => 'site',
            ),
            'categories' => array(
                'label' => 'Categories',
                'class' => 'categories',
                'uri' => '#',
            ),
            'contact-us' => array(
                'label' => 'Contact Us',
                'route' => 'site/contact-us',
            ),
        ),
    ),
);


and in my Module.php I have function:

public function attachSubMenu($serviceManager) {
        $siteNavigation = $serviceManager->get('siteNavigation');
        $router = $serviceManager->get('router');
        $categoriesTable = $serviceManager->get('Category/Model/Table/CategoriesTable');
        $categories = $categoriesTable->fetchAll();
        $categoriesRoute = $siteNavigation->findByClass('categories');
        if (!is_null($categoriesRoute)) {
            $pages = array();
            foreach ($categories as $category) {
                $newPage = new Mvc(array(
                    'label' => $category->name,
                    'route' => 'site/categories',
                    'params' => array(
                        'category' => $category->route
                    ),
                ));
                $newPage->setRouter($router);

                array_push($pages, $newPage);
            }
            $categoriesRoute->addPages($pages);
        }
    }


So far so good. Now I have menu with Category item and list with categories as subMenu of Category. But when I click one of the listed categories, neither the submenu nor parent element has class "active". When I click on Home, I have class "active", so the problem is somewhere in the category listing, in my opinion.

This is how I show navigation in layout:

<?= $this->navigation('siteNavigation')->menu()->setMaxDepth(1); ?>

I debugged and found that when I generate dynamically these categories in Zend\Navigation\Page\Mvc -> isActive function $this->getRoute() doesn't return category route... If I add categories like that:

 return array(
        'navigation' => array(
            'site' => array(
                array(
                    'label' => 'Home',
                    'route' => 'site',
                ),
                'categories' => array(
                    'label' => 'Categories',
                    'class' => 'categories',
                    'uri' => '#',
                    'pages' => array(
                         array(
                            'label' => 'Cat1',
                            'route' => 'site/categories',
                            'params' => array(
                                'category' => 'cat1'
                             ),
                      //.....
                        ),
                    ),
                ),
                'contact-us' => array(
                    'label' => 'Contact Us',
                    'route' => 'site/contact-us',
                ),
            ),
        ),
    );


everything works fine. subMenu and parent menu has class "active"....

Any idea how to fix this issue?
    

asked Oct 19, 2015 by atulpariharmca
0 votes
20 views



Related Hot Questions

1 Answer

0 votes

I thnink I found the solution...

RouteMatch was not set in the new pages, so I added one more line after:

$newPage->setRouter($router);
$newPage->setRouteMatch($routeMatch);//The new line;

Then my navigation work as expected.

answered Oct 19, 2015 by rajesh

...