zendframework / zend-navigation

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

Order on n-level (n>1) #3

Closed FabianKoestring closed 9 years ago

FabianKoestring commented 9 years ago

In the following example i create a navigation with two levels and then print the navigation labels with echo. As you can see i set an order for both first level pages and for two second level pages. If you look at my output you can see that only the first level pages are in right order. The second level pages arent in right order.

For every level you put in a navigation greater than level one, the order isnt changed.

Anybody of you have the same problem or can tell me what iam doing wrong? Or is this a not given functionality of this module?

Testscript

$container = new \Zend\Navigation\Navigation(
    array(
        array(
            'label' => 'ACL page 1 (guest)',
            'uri' => '#acl-guest',
            'resource' => 'nav-guest',
            'order' => 777,
            'pages' => array(
                 array(
                     'label' => 'ACL page 1.1 (foo)',
                      'uri' => '#acl-foo',
                      'resource' => 'nav-foo',
                      'order' => 666
                  ),
                  array(
                      'label' => 'ACL page 1.2 (bar)',
                       'uri' => '#acl-bar',
                       'resource' => 'nav-bar',
                  ),
                  array(
                      'label' => 'ACL page 1.3 (baz)',
                       'uri' => '#acl-baz',
                       'resource' => 'nav-baz',
                  ),
                  array(
                      'label' => 'ACL page 1.4 (bat)',
                      'uri' => '#acl-bat',
                      'resource' => 'nav-bat',
                      'order' => 111
                  ),
              ),
          ),
          array(
              'label' => 'ACL page 2 (member)',
              'uri' => '#acl-member',
              'resource' => 'nav-member',
              'order' => 555
          )
      )
  );

  foreach ($container as $page) {
      echo $page->label."<br>";
      if ($page->hasPages()) {
          foreach ($page->getPages() as $page2) {
              echo $page2->label."<br>";
          }
      }
  }
  die("ASD");

Output

ACL page 2 (member)
ACL page 1 (guest)
ACL page 1.1 (foo)
ACL page 1.2 (bar)
ACL page 1.3 (baz)
ACL page 1.4 (bat)
ASD
FabianKoestring commented 9 years ago

@froschdesign - Are you the maintainer?

froschdesign commented 9 years ago

@FabianKoestring No, but I worked on the component since version 1.8.

I'll look into it.

froschdesign commented 9 years ago

@FabianKoestring Here is the problem:

foreach ($page->getPages() as $page2) {}

getPages() is wrong. Use the page directly, because page is a RecursiveIterator itself.

foreach ($page as $subPage) {}

Here is an unit test:

public function testIterationWithChildrenShouldBeOrderAware()
{
    $nav = new Zend_Navigation(
        [
            [
                'label' => 'Page 1',
                'uri'   => '#',
            ],
            [
                'label' => 'Page 2',
                'uri'   => '#',
                'order' => -1,
                'pages' => [
                    [
                        'label' => 'Page 2.1',
                        'uri'   => '#',
                        'order' => 3,
                    ],
                    [
                        'label' => 'Page 2.2',
                        'uri'   => '#',
                        'order' => 2,
                    ],
                    [
                        'label' => 'Page 2.3',
                        'uri'   => '#',
                        'order' => 1,
                    ],
                ],
            ],
            [
                'label' => 'Page 3',
                'uri'   => '#',
            ],
            [
                'label' => 'Page 4',
                'uri'   => '#',
                'order' => 100,
            ],
            [
                'label' => 'Page 5',
                'uri'   => '#',
            ],
        ]
    );

    $expected = [
        'Page 2',
        'Page 2.3',
        'Page 2.2',
        'Page 2.1',
        'Page 1',
        'Page 3',
        'Page 5',
        'Page 4',
    ];
    $actual = array();
    foreach ($nav as $page) {
        $actual[] = $page->getLabel();
        if ($page->hasChildren()) {
            foreach ($page as $subPage) {
                $actual[] = $subPage->getLabel();
            }
        }
    }
    $this->assertEquals($expected, $actual);
}

Test result: Okay

FabianKoestring commented 9 years ago

@froschdesign

Oh no. Thank you very much. Works like a charm!

froschdesign commented 9 years ago

@FabianKoestring You are welcome.