MeuMySQL Blog is now at http://blog.mdnsolutions.com/.

Wednesday, June 6, 2012

[OFF-Topic] Customizando ZF BreadCrumb

Olá galera. Hoje trago um tópico que achei bacana para um problema que me confrontei hoje. E como  soluções surgem de necessidades aqui está um que me ajudou e que acredito que pode ajudar outra pessoa.


Breadcrumbs

O Zend Framework trabalha de forma elegante com navigation e breadcrumbs. Já vinha utilizando ambos há um bom tempo em meus sistemas, porém hoje precisei customizar um para um sistema especifico que possuo. Sem mais rodeios segue abaixo as intervenções que fiz no meu Layout e no meu breadcrumb.


No seu Layout.phtml adicione a linha:

<?php echo $this->navigation()->breadcrumbs()->setPartial('breadcrumb.phtml')->render(); ?>

E crie seu breadcrumb no diretório seuModulo/views/scripts/breadcrumb.phtml e escreva ele como desejar. Ex o meu:

<?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 = $active;
} else {
    $html = $active->getLabel();
    if ($this->breadcrumbs()->getUseTranslator() && $t = $this->breadcrumbs()->getTranslator()) {
        $html = $t->translate($html);
    }
    $html = $this->escape($html);
}

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

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

    $active = $parent;
}
echo strlen($html) ? $this->breadcrumbs()->getIndent() . $html : '';

?>


Bem, é isso galera. Espero que tenham gostado da dica.