ZF2 - Active <li> in Zend Navigation Menu - php

I've managed to generate menu using Zend Navigation. However, the active page is never set (active class is not set for any <li> element).
My partial:
foreach ($pages as $page): ?>
<?php if (!$page->isVisible() || !$this->navigation()->menu()->accept($page)) continue; ?>
<li role="presentation" <?php if ($page->isActive()) echo 'class="active"' ?>>
<a href="<?php echo $page->getHref() ?>">
<?php if ($icon = $page->get('icon')) {
echo '<span class="' . $icon . '"></span>';
} ?>
<span> <?php echo $this->translate($page->getLabel()) ?> </span>
</a>
</li>
<?php endforeach ?>
Extract of module.config.php:
'navigation' => array(
'default' => array(
array(
'label' => 'Page 1',
'route' => 'application/default',
'namespace' => 'Application\Controller',
'controller' => 'Index',
'action' => 'page1',
'icon' => 'fa fa-2x fa-file-text',
'order' => 10,
),
array(
'label' => 'Page 2',
'route' => 'application/default',
'namespace' => 'Application\Controller',
'controller' => 'Index',
'action' => 'page2',
'icon' => 'fa fa-2x fa-file-text',
'order' => 20,
),
),
),
The menu is rendered properly on the page, but without any active class:
$partial = array('partial/menu.phtml', 'default');
echo $this->navigation('navigation')
->menu()
->setMinDepth(0)
->setMaxDepth(0)
->setPartial($partial);
After some research into ZF code, I've found something I don't understand (in Zend\View\Helper\Navigation\Menu.php):
// in renderNormalMenu function, line 288
$isActive = $page->isActive(true);
Any idea or suggestion regarding my problem?
Thanks a lot,

Problem was in module.config.php ; the isActive method (from Zend\Navigation\Mvc) was expected the "full" controller name (including namespace).
My config was splitting namespace and controller name, wich causes the issue.
Solution:
array(
'label' => 'Page 1',
'route' => 'application/default',
'controller' => 'Application\Controller\Index',
'action' => 'page1',
'icon' => 'fa fa-2x fa-file-text',
'order' => 10,
),

Related

Dropdown menus in MVC approach

I have been trying to understand this existing php code that displays drop down menus with their subsequent menus. while the code works in one level, i am having a hard time how to or what to add another level of the dropdown... i have been able to do that but in this case where php is used to display these fields, it's kind of mind-boggling for me... here's the existing code in the view side -->
<ul class="nav navbar-nav">
<?php
foreach ($nav as $item) {
if (empty($item['children'])) {
?>
<li<?php if (!empty($item['is_active'])) { ?> class="active"<?php } ?>>
<a href="<?php echo $item['link']; ?>"><?php if ($item['icon'] != '') { ?>
<i class="<?php echo $item['icon']; ?>" data-toggle="tooltip" data-placement="bottom" data-original-title="A large tooltip."></i> <?php } echo $item['label']; ?></a></li>
<?php
}
else
{
?>
<li class="dropdown <?php if (!empty($item['is_active'])) { ?> active<?php } ?>">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" data-hover="dropdown">
<?php if ($item['icon'] != '') { ?>
<i class="<?php echo $item['icon']; ?>"></i>
<?php echo $item['label']; ?>
<b class="caret"></b><?php } ?>
</a>
<ul class="dropdown-menu">
<?php
_main_menu_widget_display_children($item['children']);
?>
</ul>
</li>
<?php
}
}
?>
and the controller looks like this -->
$nav['settings'] = array('label' => 'Settings', 'icon' => 'fa fa-wrench fa-lg', );
$nav['settings/access_rights'] = array('label' => 'Approver', 'icon' => 'fa fa-wrench fa-lg',
'location' => site_url('admin/access_rights'), 'parent_id' => 'settings');
//separator
$nav['settings/separator_1'] = array('blank' => true, 'parent_id' => 'settings');
$nav['settings/billing_param'] = array('label' => 'Technician', 'icon' => 'fa fa-wrench fa-lg',
'location' => site_url('billing_param'), 'parent_id' => 'settings');
//separator
$nav['settings/separator_2'] = array('blank' => true, 'parent_id' => 'settings');
$nav['settings/support_settings'] = array('label' => 'Support', 'icon' => 'fa fa-wrench fa-lg',
'location' => site_url('support_settings'), 'parent_id' => 'settings');
//sub-menu sub of Support
$nav['example1'] = array('label' => 'Request Supportt', 'icon' => 'fa fa-wrench fa-lg',
'location' => '#', 'parent_id' => 'settings/support_settings');
$nav['example2'] = array('label' => 'Need Features', 'icon' => 'fa fa-wrench fa-lg',
'location' => '#', 'parent_id' => 'settings/support_settings');
$nav['example3'] = array('label' => 'Telephone Number', 'icon' => 'fa fa-wrench fa-lg',
'location' => '#', 'parent_id' => 'settings/support_settings');
$nav['example4'] = array('label' => 'Testimonials', 'icon' => 'fa fa-wrench fa-lg',
'location' => '#', 'parent_id' => 'settings/support_settings');
$nav['example6'] = array('label' => 'Known Issues', 'icon' => 'fa fa-wrench fa-lg',
'location' => '#', 'parent_id' => 'settings/support_settings');
$nav['example5'] = array('label' => 'Road Map', 'icon' => 'fa fa-wrench fa-lg',
'location' => '#', 'parent_id' => 'settings/support_settings');
here's what it looks like :
i just wanted those list under 'support' not shown when i clicked the 'settings' menu but rather after i hover or clicked (it doesnt matter really) the 'support' sub-menu...
i know there's something missing in the 'view' side but i dont know what is... another if-else maybe but i cant see any connection so far... any help is very much appreciated thanks... :)

Separate navigation config for zend framework modules

I have the following configuration for my navigation in my application module in Application/config/module.config.php:
'navigation' => array(
'default' => array(
array(
'label' => 'Home',
'route' => 'home'
)
)
)
I have the following configuration for my navigation in my cortana module in Cortana/config/module.config.php:
'navigation' => array(
'default' => array(
array(
'label' => 'Home',
'route' => 'cortana-home'
),
array(
'label' => 'Resources',
'route' => 'cortana-resources'
),
array(
'label' => 'Reports',
'route' => 'cortana-reports'
),
array(
'label' => 'Services',
'route' => 'cortana-services'
),
array(
'label' => 'System',
'route' => 'cortana-system'
)
)
)
Im using the layout in my Cortana modulle. Here is the navigation code:
<div class="collapse navbar-collapse">
<?php echo $this->navigation('navigation')
->menu()
->setMinDepth(0)
->setMaxDepth(0)
->setUlClass('nav navbar-nav');
?>
</div><!--/.nav-collapse -->
And finally, unfortunately my result compiles the following HTML:
<li>
Home
</li>
<li class="active">
Home
</li>
<li>
Resources
</li>
<li>
Reports
</li>
<li>
Services
</li>
<li>
System
</li>
</ul> </div><!--/.nav-collapse -->
As you can see, there is 2 home navigation pages. It looks like the code merged the Application navigation module and the Cortana navigation module.
What I need is to have a navigation for the Cortana module and then a separate navigation for the Application module.
UPDATE
I've also tried changing the navigation key to cortana-navigation the following:
/Cortana/config/module.config.php
'service_manager' => array(
'factories' => array(
'cortana-navigation' => 'Zend\Navigation\Service\DefaultNavigationFactory'
)
),
'cortana-navigation' => array(
'default' => array(
array(
'label' => 'Home',
'route' => 'cortana-home'
),
array(
'label' => 'Resources',
'route' => 'cortana-resources'
),
array(
'label' => 'Reports',
'route' => 'cortana-reports'
),
array(
'label' => 'Services',
'route' => 'cortana-services'
),
array(
'label' => 'System',
'route' => 'cortana-system'
)
)
)
And then in my Cortana layout (Cortana/view/layout/layout.phtml)
<div class="collapse navbar-collapse">
<?php echo $this->navigation('cortana-navigation')
->menu()
->setMinDepth(0)
->setMaxDepth(0)
->setUlClass('nav navbar-nav');
?>
</div><!--/.nav-collapse -->
And now my result compiled HTML is:
<ul class="nav navbar-nav">
<li>
Home
</li>
</ul> </div><!--/.nav-collapse -->
I unfortunately have no explanation for why its now using the Application navigation (navigation) instead of the (cortana-navigation) but it does mean that its responding to the change of factory
SECOND UPDATE
I did notice that in the file `Zend/Navigaiton/Service/DefaultNavigationFactory.php' :
class DefaultNavigationFactory extends AbstractNavigationFactory
{
/**
* #return string
*/
protected function getName()
{
return 'default';
}
}
This might mean something with using the default name

Add 2 tags on link in cakephp

I am new in cakephp and i have to create a link with 2 tags like following html
<li><i class="fa fa-th-list"></i> <span>Shop</span></li>
And in cake php i did something like this:
<li><?php echo $this->Html->link(
// $this->Html->tag('i', array('class' => array('fa', 'fa-th-list'))),
$this->Html->tag('span', 'Video / Imagini', null),
array(
'controller' => 'users',
'action' => 'video',
),
array('escape' => FALSE)) ?></li>
How can i add the tag?
I search to internet and on cake book but no details about the second tag.
Thank you for your time.
Your code was almost correct. I made only some little changes:
<li><?php
echo $this->Html->link(
$this->Html->tag('i', '', array('class' => array('fa', 'fa-th-list'))) .
$this->Html->tag('span', 'Video / Imagini'),
array(
'controller' => 'users',
'action' => 'video',
),
array('escape' => false)
);
?></li>

links__system_main_menu in drupal

what is the use of links__system_main_menu in drupal?
<?php if ($main_menu): ?>
<div id="main-menu" class="navigation">
<?php print theme('links__system_main_menu', array(
'links' => $main_menu,
'attributes' => array(
'id' => 'main-menu-links',
'class' => array('links', 'clearfix'),
),
'heading' => array(
'text' => t('Main menu'),
'level' => 'h2',
'class' => array('element-invisible'),
),
)); ?>
</div> <!-- /#main-menu -->
This is a pattern for theme hook, in the form [base hook]__[context]. When links theme with theme('links__system_main_menu', $variables), theme() function search for *theme_links__system_main_menu()* and use it. Otherwise, if it doesn't find, it will use *theme_links()*. For more information check the theme Doc

Zend Framework 2 Navigation Submenu (Top menu)

I'm building a menu using ZF2 and bootstrap and I already have this kind of menu:
Home | Users | Options
But now I need submenus, but I couldn't find a way to do so. I need something like hover the menu item (Ex: User) and then show 'List', 'Add', 'Edit'
I would really appreciate any help.
Thanks
You can achieve this with a partial view.
In you configuration file e.g. config/autoload/global.php :
return array(
// Your others config arrays
'navigation' => array(
'default' => array(
array(
'label' => 'Home',
'route' => 'home',
),
array(
'label' => 'User',
'route' => 'user',
'pages' => array(
array(
'label' => 'List',
'route' => 'list',
),
array(
'label' => 'Add',
'route' => 'add',
),
array(
'label' => 'Edit',
'route' => 'edit',
),
),
),
array(
'label' => 'Options',
'route' => 'options',
),
)
)
);
In your layout file, e.g. view/layout/layout.phtml :
<nav>
<?php
echo $this->navigation('navigation')
->menu()
->setPartial('partial/menu')
->render();
?>
</nav>
The partial view, here view/partial/menu.phtml :
<ul>
<?php
foreach ($this->container as $page)
{
$hasChildren = $page->hasPages();
if( ! $hasChildren)
{
?>
<li><?php echo $page->getLabel(); ?></li>
<?php
}
else
{
?>
<li>
<?php echo $page->getLabel(); ?>
<ul>
<?php
foreach($page->getPages() as $child)
{
?>
<li><?php echo $child->getLabel(); ?></li>
<?php
}
?>
</ul>
</li>
<?php
}
}
?>
</ul>
If you need CSS samples you can find some in this answer :
https://stackoverflow.com/a/13328340/3294723

Categories