Commits

David Lukas  committed 384f413

Navigation view helper - wip

  • Participants
  • Parent commits f2e6984

Comments (0)

Files changed (5)

 
 class Module implements AutoloaderProvider
 {
+    /**
+     * View Renderer
+     * @var \Zend\View\Renderer\PhpRenderer
+     */
+    protected $renderer;
+
+    /* ********************** METHODS ************************** */
+
     public function init(Manager $moduleManager) {
+        $events = StaticEventManager::getInstance();
+        $events->attach('bootstrap', 'bootstrap', array($this, 'onBootstrap'));
     }
 
     public function getAutoloaderConfig() {
     public function getConfig() {
         return include __DIR__ . '/config/module.config.php';
     }
+
+
+    /**
+     * OnBootstrap listener
+     * The navigation view helpers do not work 'out-of-the-box' currently,
+     * the are configured here and in the onRoute method
+     * Other information:
+     * @link http://zend-framework-community.634137.n4.nabble.com/Zend-Navigation-problem-td4256771.html
+     * @link http://mwop.net/slides/2012-04-25-ViewWebinar/Zf2Views.html#slide41
+     * @param $e
+     */
+    public function onBootstrap($e) {
+        $app      = $e->getParam('application');
+        $locator  = $app->getLocator();
+
+        //Store renderer as a property, it will be used by the onRoute() method
+        $this->renderer = $locator->get('Zend\View\Renderer\PhpRenderer');
+
+        //Register DluTwBootstrap view navigation helpers
+        $this->renderer->plugin('navigation')
+                       ->getPluginLoader()
+                       ->registerPlugin('twbNavbar', 'DluTwBootstrap\View\Helper\Navigation\TwbNavbar');
+
+        //Prepare the \Zend\Navigation\Page\Mvc for use in the navigation view helper
+        \Zend\Navigation\Page\Mvc::setDefaultUrlHelper($this->renderer->plugin('url'));
+
+        //Attach a listener to the app route event (to configure the url view helper)
+        $app->events()->attach('route', array($this, 'onRoute'));
+    }
+
+    /**
+     * OnRoute listener
+     * Configures the url view helper to be usable in navigation view helpers
+     * @param \Zend\Mvc\MvcEvent $e
+     */
+    public function onRoute(\Zend\Mvc\MvcEvent $e) {
+        $routeMatch      = $e->getRouteMatch();
+        $this->renderer->plugin('url')->setRouteMatch($routeMatch);
+    }
 }

File config/module.config.php

     'di'    => array(
         'instance'  => array(
             'alias'     => array(
-                'tw-bootstrap-demo'     => 'DluTwBootstrap\Controller\DemoController',
-                'nav-menu-main'         => 'Zend\Navigation\Navigation',
+                'tw-bootstrap-demo'         => 'DluTwBootstrap\Controller\DemoController',
+                'dlutwb-nav-menu-main'      => 'Zend\Navigation\Navigation',
             ),
             'Zend\View\Resolver\TemplatePathStack'  => array(
                 'parameters'    => array(
                     'paths'         => array(
                         'dluTwBootstrap' => __DIR__ . '/../view',
+                        'partials'       => __DIR__ . '/../view/partials',
                     ),
                 ),
             ),
             'tw-bootstrap-demo'         => array(
                 'parameters'    => array(
-                    'navbar'    => 'nav-menu-main',
+                    'navMenuMain'        => 'dlutwb-nav-menu-main',
                 ),
             ),
-            'nav-menu-main'             => array(
+            'dlutwb-nav-menu-main'  => array(
                 'parameters'    => array(
                     'pages' => array(
                         array(
                                     'action'        => 'form'
                                 ),
                                 array(
-                                    'label'         => 'NavBar',
-                                    'title'         => 'NavBar Demo',
+                                    'label'         => 'Navigation',
+                                    'title'         => 'Navigation Demo',
                                     'module'        => 'DluTwBootstrap',
                                     'controller'    => 'tw-bootstrap-demo',
-                                    'action'        => 'navbar',
+                                    'action'        => 'navigation',
                                 ),
                             ),
                         ),
+                        array(
+                            'label'     => 'A link',
+                            'uri'       => 'http://www.zend.com',
+                        ),
+                        array(
+                            'label'         => 'Navigation',
+                            'title'         => 'Navigation Demo',
+                            'module'        => 'DluTwBootstrap',
+                            'controller'    => 'tw-bootstrap-demo',
+                            'action'        => 'navigation',
+                        ),
                     ),
                 ),
             ),

File src/DluTwBootstrap/Controller/DemoController.php

 class DemoController extends ActionController
 {
     /**
-     * NavBar
+     * Navigation - main menu navbar
      * @var \Zend\Navigation\Navigation
      */
-    protected $navbar;
+    protected $navMenuMain;
 
     /* ***************************** METHODS ****************************** */
 
     }
 
     /**
-     * Navbar action
+     * Navigation action
      * @return \Zend\View\Model\ViewModel
      */
-    public function navbarAction() {
-        \Zend\Debug::dump($this->navbar);
-        die('Check');
+    public function navigationAction() {
+        //\Zend\Debug::dump($this->navMenuMain);
+        //die('Check');
         $viewModel      = new ViewModel(array(
+            'navMenuMain'  => $this->navMenuMain,
         ));
         return $viewModel;
     }
 
-    public function setNavbar(\Zend\Navigation\Navigation $navbar) {
-        $this->navbar   = $navbar;
+    /**
+     * Sets the main menu
+     * (Setter injection)
+     * @param \Zend\Navigation\Navigation $navMenuMain
+     */
+    public function setNavMenuMain(\Zend\Navigation\Navigation $navMenuMain) {
+        $this->navMenuMain   = $navMenuMain;
     }
 }

File src/DluTwBootstrap/View/Helper/Navigation/TwbNavbar.php

+<?php
+namespace DluTwBootstrap\View\Helper\Navigation;
+
+class TwbNavbar extends \Zend\View\Helper\Navigation\AbstractHelper
+{
+
+    /* *********************** METHODS *************************** */
+
+    /**
+     * Renders helper
+     * @param  \Zend\Navigation\Container $container [optional] container to render.
+     *                                         Default is null, which indicates
+     *                                         that the helper should render
+     *                                         the container returned by {@link
+     *                                         getContainer()}.
+     * @return string helper output
+     * @throws \Zend\View\Exception if unable to render
+     */
+    public function render(\Zend\Navigation\Container $container = null) {
+        if (null === $container) {
+            $container = $this->getContainer();
+        }
+
+        $html   = '';
+        $html   .= '<div class="navbar">';
+        $html   .= "\n" . '<div class="navbar-inner">';
+        $html   .= "\n" . '<div class="container">';
+
+        $ul     = $this->getUlFromContainer($container);
+        $html   .= "\n" . $ul;
+
+        $html   .= "\n</div>";
+        $html   .= "\n</div>";
+        $html   .= "\n</div>";
+
+        return $html;
+    }
+
+    protected function getUlFromContainer(\Zend\Navigation\Container $container) {
+        $html   = '<ul class="nav">';
+        $pages  = $container->getPages();
+        foreach ($pages as $page) {
+            /* @var $page \Zend\Navigation\Page\AbstractPage */
+            if($page->hasPages()) {
+                //A dropdown menu
+                $html   .= "\n" . '<li class="dropdown">';
+                $html   .= "\n" . '<a href="#" class="dropdown-toggle" data-toggle="dropdown">' . $page->getLabel() . '<b class="caret"></b></a>';
+                $html   .= "\n" . '<ul class="dropdown-menu">';
+                foreach ($page->getPages() as $child) {
+                    $html   .= "\n" . $this->renderLink($child);
+                }
+                $html   .= "\n</ul>";
+                $html   .= "\n</li>";
+            } else {
+                //A link
+                $html   .= "\n" . $this->renderLink($page);
+            }
+        }
+        $html   .= "\n</ul>";
+        return $html;
+    }
+
+    protected function renderLink(\Zend\Navigation\Page\AbstractPage $page) {
+        //TODO - active is not working!
+        if($page->isActive()) {
+            $liClass    = ' class="active"';
+        } else {
+            $liClass    = '';
+        }
+        $html   = '<li' . $liClass . '><a href="' . $page->getHref() . '">' . $page->getLabel() . '</a></li>';
+        return $html;
+    }
+
+    /**
+     * View helper entry point:
+     * Retrieves helper and optionally sets container to operate on
+     * @param  \Zend\Navigation\Container $container [optional] container to operate on
+     * @return TwbNavbar    fluent interface, returns self
+     */
+    public function __invoke(\Zend\Navigation\Container $container = null) {
+        if (null !== $container) {
+            $this->setContainer($container);
+        }
+        return $this;
+    }
+}

File view/tw-bootstrap-demo/navigation.phtml

+<?php
+//echo $this->navigation()->menu($navMenuMain);
+echo $this->navigation()->twbNavbar($navMenuMain);
+?>
+
+<h2>Navigation</h2>
+<p><i>You may find other useful information in my <a href="http://www.zfdaily.com">ZF Daily</a> blog post
+    <a href="http://www.zfdaily.com/2012/04/twitter-bootstrap-forms-with-zf2-easily">Twitter Bootstrap Forms with ZF2. Easily.</a>
+</i></p>
+<ul>
+    <li><a href="#navbar-demo">Navbar</a></li>
+</ul>
+<?php
+
+?>