zendframework / zend-navigation

Navigation component from Zend Framework
BSD 3-Clause "New" or "Revised" License
20 stars 25 forks source link

Zend\Navigation\AbstractContainer->hasChildren() does not seem to work correctly #58

Closed andreidiaconescu closed 7 years ago

andreidiaconescu commented 7 years ago

Hello,

After trying for a few hours to make the zend navigation work, when upgrading from ZF2(2.4) to ZF3, i think i have found a problem with method: Zend\Navigation\AbstractContainer->hasChildren() In the past it used to work like this (in ZF 2.4):

Is this a problem or not ?

Thank you !

froschdesign commented 7 years ago

@andreidiaconescu Can you provide a short code example to reproduce the problem? (Like this one.)

Thanks!

andreidiaconescu commented 7 years ago

The piece of code which may have a problem is: Zend\Navigation\AbstractContainer::hasChildren()

public function hasChildren()
{
    return $this->valid() && $this->current()->hasPages();
}

The method current() returns: return $this->pages[$hash];

So $this->current()->hasPages() returns true if current child of current page has pages;

Providing a code example is a bit harder, as the app is a bit large, and for navigation, it has a factory, navigation template, another the template where helper is used show the menu, etc. But if really necessary, i will give a sample of code as is used in the app.

Is what i wrote enough or you need a code example ?

andreidiaconescu commented 7 years ago

code sample:

  1. template where navigation view helper is used:

    $this->navigation('admin_navigation')->menu()->setPartial(array('partials/admin-menu', ''));
    echo $this->navigation('admin_navigation')->menu();
  2. pages configuration array:

    
    <?php
    return array(
    // All navigation-related configuration is collected in the 'navigation' key
    'navigation' => array(
        'admin' => array(
            'list-texts' => array(
                'label' => 'Text blocks',
                'route' => 'backend',
                'icon-name' => 'fa fa-file-text-o',
                'params' => array('controller' => 'text', 'action' => 'index'),
            ),
            'sys' => array(
                'label' => 'System',
                'route' => 'backend',
                'icon-name' => 'fa fa-cogs',
                'params' => array('controller' => 'user', 'action' => 'index'),
                'pages' => array(
                    'users' => array(
                        'label' => 'Users',
                        'route' => 'backend',
                        'icon-name' => 'fa fa-user',
                        'params' => array('controller' => 'user', 'action' => 'index'),
                    ),
                    'list-labels' => array(
                        'label' => 'Labels',
                        'route' => 'backend',
                        'icon-name' => 'fa fa-book',
                        'params' => array('controller' => 'label', 'action' => 'index'),
                    ),
                )
            ),
        ),
    ),
    );

3. partial for the navigation

<?php $i = 0; foreach ($this->container as $page) { $i++; ?>

  • hasChildren()) { ?>
  • <?php } ?>

    froschdesign commented 7 years ago

    Here you can find an old related issue: https://github.com/zendframework/zendframework/issues/4517

    froschdesign commented 7 years ago

    And a working example:

    <?php
    /**
     * @var \Zend\View\Renderer\PhpRenderer $this
     * @var \Zend\Navigation\Navigation     $container
     */
    ?>
    <ul>
        <?php foreach ($container as $page): ?>
            <li>
                <?= $page->getLabel() ?>
    
                <?php if ($page->hasPages()): ?>
                    <ul class="sub-menu">
                        <?php foreach ($page as $child): ?>
                            <li>
                                <?= $child->getLabel() ?>
                            </li>
                        <?php endforeach ?>
                    </ul>
                <?php endif ?>
            </li>
        <?php endforeach ?>
    </ul>
    froschdesign commented 7 years ago

    Btw. you can simplify this:

    $this->navigation('admin_navigation')->menu()->setPartial(array('partials/admin-menu', ''));
    echo $this->navigation('admin_navigation')->menu();

    with:

    <?= $this->navigation('admin_navigation')->menu()->setPartial('partials/admin-menu') ?>
    andreidiaconescu commented 7 years ago

    thanks a lot !