Home > Computing, PHP, Zend Framework > Using a custom view helper in Zend Framework to create breadcrumbs

Using a custom view helper in Zend Framework to create breadcrumbs

December 15th, 2009 Leave a comment Go to comments

I don’t think I am the only one who needs to put breadcrumbs on a page with zend framework. So I’ll share my helper with you. You can use this helper in you views to set the breadcrumbs.

You can use this helper like you use the HeadTitle helper, it is based on that.
So, in you layout, select which separator you want to use, and if you want to use automatic escaping or not (Can’t use this if you use urls). You can also set a prefix, postfix and indent if you want, just like with the HeadTitle helper. You can set, append and prepend items to the breadcrumb trail.

This is the code for the layout and also an example of prepending an item to the breadcrumb:

1
2
3
4
5
6
7
8
9
10
  <?php
    // Set the separator for the breadcrumbs.
    $this->breadCrumb()->setSeparator(' > ');
    // Disable the auto escaping of html.
    $this->breadCrumb()->setAutoEscape(false);
    // Set the first (default) element of the breadcrumbs.
    $this->breadCrumb("Home", "/", Zend_View_Helper_Placeholder_Container_Abstract::PREPEND);
    // Display the breadcrumb.
    echo $this->breadCrumb();
    // ...

This is an example of the code for a view script:

1
2
3
4
5
<?php
// Set breadcrumbs for this view.
$this->breadCrumb($this->placeholder('category_name'), $this->url(array('category' => $this->placeholder('category_url_name')), 'category_route', true));
$this->breadCrumb($this->placeholder('subject_name'));
// ...

The full helper:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
/** Zend_View_Helper_Placeholder_Container_Standalone */
require_once 'Zend/View/Helper/Placeholder/Container/Standalone.php';
 
/**
 * Helper for setting and retrieving breadcrumbs for HTML.
 * Based on the HeadTitle helper.
 *
 * @version 1.0
 * @uses Zend_View_Helper_Placeholder_Container_Standalone
 */
class Zend_View_Helper_BreadCrumb extends Zend_View_Helper_Placeholder_Container_Standalone
{
    /**
     * Registry key for placeholder
     * @var string
     */
    protected $_regKey = 'Zend_View_Helper_BreadCrumb';
 
    /**
     * Whether or not auto-translation is enabled
     * @var boolean
     */
    protected $_translate = false;
 
    /**
     * Translation object
     *
     * @var Zend_Translate_Adapter
     */
    protected $_translator;
 
    /**
     * Container for the urls of the items.
     * 
     * @var Zend_View_Helper_Placeholder_Container
     */
    protected $_urls;
 
    /**
     * Retrieve placeholder for title element and optionally set a new item.
     *
     * @param  string $title
     * @param  string $url
     * @param  string $setType
     * @param  string $separator
     * @return Zend_View_Helper_BreadCrumb
     */
    public function breadCrumb($title = null, $url = null, $setType = Zend_View_Helper_Placeholder_Container_Abstract::APPEND)
    {
        // Initialize the url placeholder container if it is not created yet.
        if(!is_object($this->_urls)) $this->_urls = new Zend_View_Helper_Placeholder_Container();
        if ($title) {
            // Title must be given.
            if ($setType == Zend_View_Helper_Placeholder_Container_Abstract::SET) {
                // Reset the objects and set the first item.
                $this->set($title);
                $this->_urls->set($url);
            } elseif ($setType == Zend_View_Helper_Placeholder_Container_Abstract::PREPEND) {
                // Prepend an item to the breadcrumb.
                $this->prepend($title);
                $this->_urls->prepend($url);
            } else {
                // Append an item to the breadcrumb.
                $this->append($title);
                $this->_urls->append($url);
            }
        }
        // Return self for chaining and display of the breadcrumbs.
        return $this;
    }
 
    /**
     * Sets a translation Adapter for translation
     *
     * @param  Zend_Translate|Zend_Translate_Adapter $translate
     * @return Zend_View_Helper_BreadCrumb
     */
    public function setTranslator($translate)
    {
        if ($translate instanceof Zend_Translate_Adapter) {
            $this->_translator = $translate;
        } elseif ($translate instanceof Zend_Translate) {
            $this->_translator = $translate->getAdapter();
        } else {
            require_once 'Zend/View/Exception.php';
            throw new Zend_View_Exception("You must set an instance of Zend_Translate or Zend_Translate_Adapter");
        }
        return $this;
    }
 
    /*
     * Retrieve translation object
     *
     * If none is currently registered, attempts to pull it from the registry
     * using the key 'Zend_Translate'.
     *
     * @return Zend_Translate_Adapter|null
     */
    public function getTranslator()
    {
        if (null === $this->_translator) {
            require_once 'Zend/Registry.php';
            if (Zend_Registry::isRegistered('Zend_Translate')) {
                $this->setTranslator(Zend_Registry::get('Zend_Translate'));
            }
        }
        return $this->_translator;
    }
 
    /**
     * Enables translation
     *
     * @return Zend_View_Helper_BreadCrumb
     */
    public function enableTranslation()
    {
        $this->_translate = true;
        return $this;
    }
 
    /**
     * Disables translation
     *
     * @return Zend_View_Helper_BreadCrumb
     */
    public function disableTranslation()
    {
        $this->_translate = false;
        return $this;
    }
 
    /**
     * Turn helper into string to echo.
     *
     * @param  string|null $indent
     * @param  string|null $locale
     * @return string
     */
    public function toString($indent = null, $locale = null)
    {
        $indent = (null !== $indent) ? $this->getWhitespace($indent) : $this->getIndent();
 
        $items = array();
        if($this->_translate && $translator = $this->getTranslator()) {
            // Translator is set.
            for($i = 0; $i < $this->count(); $i++) {
                // Loop through all breadcrumb items.
                // Get item and its url.
                $item = $this->offsetGet($i);
                $url = $this->_urls->offsetGet($i);
                if(null === $url) {
                    // No url is set, add an url-less item.
                    $items[] = $translator->translate($item, $locale);
                } else {
                    // Url is set, create HTML.
                    $items[] = '<a href="'.$url.'">'.$translator->translate($item, $locale).'</a>';
                }
            }
        } else {
            // No translation required.
            for($i = 0; $i < $this->count(); $i++) {
                // Loop through all breadcrumb items.
                // Get item and its url.
                $item = $this->offsetGet($i);
                $url = $this->_urls->offsetGet($i);
                if(null === $url) {
                    // No url is set, add an url-less item.
                    $items[] = $item;
                } else {
                    // Url is set, create HTML.
                    $items[] = '<a href="'.$url.'">'.$item.'</a>';
                }
            }
        }
        // Fetch set separator.
        $separator = $this->getSeparator();
        $output = '';
        // Check if a prefix was set.
        if(($prefix = $this->getPrefix())) {
            // Add prefix to beginning of string.
            $output  .= $prefix;
        }
        // Implode all items and append them to the string.
        $output .= implode($separator, $items);
        // Check if a postfix was set.
        if(($postfix = $this->getPostfix())) {
            // Add a postfix to the end of the string.
            $output .= $postfix;
        }
        // Check if escaping is enabled (should be disabled when using urls).
        $output = ($this->_autoEscape) ? $this->_escape($output) : $output;
        // Return the final string with breadcrumbs.
        return $indent . $output;
    }
}
  1. June 9th, 2011 at 11:45 | #1

    Great helper. Thanks! I just added class=”last” to the last element.

  1. No trackbacks yet.