1

I have some trouble making a customized breadcrumb: I my layout.phtml I have:

$container = new Zend_Navigation();
$this->navigation($container);

$container->addPage(
array(
    'label'      => 'Dashboard',
    'module'     => 'default',
    'controller' => 'dashboard',
    'action'     => 'index',
    'pages'      =>
    array(
        array(
            'label'      => 'Create Order',
            'module'     => 'default',
            'controller' => 'createorder',
            'action'     => 'index'
        ),
        array(
            'label'      => 'Query',
            'module'     => 'default',
            'controller' => 'query',
            'action'     => 'index',
            'pages' => array(
                array(
                    'label'      => 'View Order',
                    'module'     => 'default',
                    'controller' => 'order',
                    'action'     => 'vieworder'
                )
            )
        ),
        array(
            'label'      => 'Administration',
            'module'     => 'default',
            'controller' => 'admin',
            'action'     => 'index',
            'pages'      =>
            array(
                array(
                    'label'      => 'News and Announcements',
                    'module'     => 'default',
                    'controller' => 'admin',
                    'action' => 'addnews',
                    'pages' => array(
                        array(
                            'label'      => 'Edit News and Announcements',
                            'module'     => 'default',
                            'controller' => 'admin',
                            'action' => 'editnews'
                        )
                    )
                )
            )
        )
    )
)
);

The next line is:

echo $this->navigation()->breadcrumbs()->setPartial(array('BreadCrumb.phtml','default'));

The BreadCrumb.phtml is called well, but I don't know how to make an ul-li menu in my BreadCrumb.phtml. How do I get the navigation I'm actually in? Thanks in advance for any help. Andrea

3 Answers 3

4

To do so, your BreadCrumb.phtml should look like this:

<?php
if (null === $this->container) {
    $this->container = $this->breadcrumbs()->getContainer();
}

// find deepest active
if (!$active = $this->breadcrumbs()->findActive($this->container)) {
    return '';
}

$active = $active['page'];

// put the deepest active page last in breadcrumbs
if ($this->breadcrumbs()->getLinkLast()) {
    $html = ' <li>' . $this->breadcrumbs()->htmlify($active) . '</li>' . PHP_EOL;
} else {
    $html = $active->getLabel();
    if ($this->breadcrumbs()->getUseTranslator() && $t = $this->breadcrumbs()->getTranslator()) {
        $html = $t->translate($html);
    }
    $html = ' <li>' . $this->escape($html) . '</li>' . PHP_EOL;
}

// walk back to root
while (($parent = $active->getParent()) != null) {
    if ($parent instanceof Zend_Navigation_Page) {
        // prepend crumb to html
        $html = ' <li>' . $this->breadcrumbs()->htmlify($parent) . '</li>' . PHP_EOL . $html;
    }

    if ($parent === $this->container) {
        // at the root of the given container
        break;
    }

    $active = $parent;
}
echo strlen($html) ? $this->breadcrumbs()->getIndent() . '<ul id="breadcrumbs">' . PHP_EOL 
                 . $html .  '</ul>' . PHP_EOL : '';
?>

Then you will get something like this:

<ul id="breadcrumbs">
    <li><a href="/home">Home</a></li>
    <li><a href="/articles">Articles</a></li>
    <li><a href="/shop">Shop</a></li>
    <li>Last link</li>
</ul>
Sign up to request clarification or add additional context in comments.

4 Comments

No problem, I improved a little bit my code to make it work even if there is nothing to render. Let me know!
This is a godd example of rendering the breadcrumbs, @Liyali ! Where did you find it? This one should be added to the official docs example - the current one is "poor": framework.zend.com/manual/2.3/en/modules/…
@Liyali Where did you find the code used in your example?
ooops zf1! Thought it's for zf2. Didn't pay attention to the tags
0

Inside the view script, the zend navigation container is retrievable via $this->container. The container is iterable, so you can walk through it with a simple foreach loop (e.g. foreach($this->container as $page)

Comments

0

I've found simple solution for render breadcrumbs wrapped in ul-li menu.

You can place this code in your breadcrumbs.phtml:

<?php $breadcrumbs = $this->navigation()->breadcrumbs()->setMinDepth(0); ?>
<?php $items = array_filter(explode($breadcrumbs->getSeparator(), $breadcrumbs->render())); ?>
<?php if ($items) : ?>
    <ul class="breadcrumbs">
        <?php foreach ($items as $item) : ?>
            <li><?= $item ?></li>
        <?php endforeach; ?>
    </ul>
<?php endif; ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.