Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

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

Closed
andreidiaconescu opened this issue Feb 9, 2017 · 7 comments
Labels

Comments

@andreidiaconescu
Copy link

andreidiaconescu commented Feb 9, 2017

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):

  • it would return true if page on which method was called had child pages;
    Now it does not work like this, but does the following:
  • it returns true if first child of current page (on which method is called) has child pages
  • this seems not ok to me, but i may be wrong;

Is this a problem or not ?

Thank you !

@froschdesign
Copy link
Member

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

Thanks!

@andreidiaconescu andreidiaconescu changed the title Zend\Navigation\AbstractContainer->hasPages() does not seem to work correctly Zend\Navigation\AbstractContainer->hasChildren() does not seem to work correctly Feb 9, 2017
@andreidiaconescu
Copy link
Author

andreidiaconescu commented Feb 9, 2017

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];

  • $this->pages is a collection of child pages of current page, which means it returns current child page.

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
Copy link
Author

andreidiaconescu commented Feb 9, 2017

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();
  1. 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'),
                    ),
                )
            ),
        ),
    ),
);

  1. partial for the navigation
<?php
    $i = 0;
    foreach ($this->container as $page) {
        $i++;
?>
      <li>

<?php

          if ($page->hasChildren()) {
?>
          <ul class="sub-menu">
              <?php foreach ($childPages as $child) : ?>
              <li>
                  .........................
              </li>
              <?php endforeach; ?>
          </ul>
<?php
          }
?>
      </li>
<?php
    }
?>


@froschdesign
Copy link
Member

Here you can find an old related issue: zendframework/zendframework#4517

@froschdesign
Copy link
Member

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
Copy link
Member

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
Copy link
Author

thanks a lot !

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants