3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\View\Helper\Navigation
;
12 use Zend\Navigation\AbstractContainer
;
13 use Zend\Navigation\Page\AbstractPage
;
15 use Zend\View\Exception
;
18 * Helper for printing breadcrumbs
20 class Breadcrumbs
extends AbstractHelper
23 * Whether last page in breadcrumb should be hyperlinked
27 protected $linkLast = false;
30 * The minimum depth a page must have to be included when rendering
34 protected $minDepth = 1;
37 * Partial view script to use for rendering menu
44 * Breadcrumbs separator string
48 protected $separator = ' > ';
53 * @param string|AbstractContainer $container container to operate on
56 public function __invoke($container = null)
58 if (null !== $container) {
59 $this->setContainer($container);
68 * Implements {@link HelperInterface::render()}.
70 * @param AbstractContainer $container [optional] container to render. Default is
71 * to render the container registered in the helper.
74 public function render($container = null)
76 $partial = $this->getPartial();
78 return $this->renderPartial($container, $partial);
81 return $this->renderStraight($container);
85 * Renders breadcrumbs by chaining 'a' elements with the separator
86 * registered in the helper
88 * @param AbstractContainer $container [optional] container to render. Default is
89 * to render the container registered in the helper.
92 public function renderStraight($container = null)
94 $this->parseContainer($container);
95 if (null === $container) {
96 $container = $this->getContainer();
99 // find deepest active
100 if (!$active = $this->findActive($container)) {
104 $active = $active['page'];
106 // put the deepest active page last in breadcrumbs
107 if ($this->getLinkLast()) {
108 $html = $this->htmlify($active);
110 $html = $active->getLabel();
111 if (null !== ($translator = $this->getTranslator())) {
112 $html = $translator->translate($html, $this->getTranslatorTextDomain());
114 $escaper = $this->view
->plugin('escapeHtml');
115 $html = $escaper($html);
119 while ($parent = $active->getParent()) {
120 if ($parent instanceof AbstractPage
) {
121 // prepend crumb to html
122 $html = $this->htmlify($parent)
123 . $this->getSeparator()
127 if ($parent === $container) {
128 // at the root of the given container
135 return strlen($html) ?
$this->getIndent() . $html : '';
139 * Renders the given $container by invoking the partial view helper
141 * The container will simply be passed on as a model to the view script,
142 * so in the script it will be available in <code>$this->container</code>.
144 * @param AbstractContainer $container [optional] container to pass to view script.
145 * Default is to use the container registered
147 * @param string|array $partial [optional] partial view script to use.
148 * Default is to use the partial registered
149 * in the helper. If an array is given, it
150 * is expected to contain two values; the
151 * partial view script to use, and the module
152 * where the script can be found.
153 * @throws Exception\RuntimeException if no partial provided
154 * @throws Exception\InvalidArgumentException if partial is invalid array
155 * @return string helper output
157 public function renderPartial($container = null, $partial = null)
159 $this->parseContainer($container);
160 if (null === $container) {
161 $container = $this->getContainer();
164 if (null === $partial) {
165 $partial = $this->getPartial();
168 if (empty($partial)) {
169 throw new Exception\
RuntimeException(
170 'Unable to render menu: No partial view script provided'
174 // put breadcrumb pages in model
175 $model = array('pages' => array());
176 $active = $this->findActive($container);
178 $active = $active['page'];
179 $model['pages'][] = $active;
180 while ($parent = $active->getParent()) {
181 if ($parent instanceof AbstractPage
) {
182 $model['pages'][] = $parent;
187 if ($parent === $container) {
188 // break if at the root of the given container
194 $model['pages'] = array_reverse($model['pages']);
197 if (is_array($partial)) {
198 if (count($partial) != 2) {
199 throw new Exception\
InvalidArgumentException(
200 'Unable to render menu: A view partial supplied as '
201 . 'an array must contain two values: partial view '
202 . 'script and module where script can be found'
206 $partialHelper = $this->view
->plugin('partial');
207 return $partialHelper($partial[0], /*$partial[1], */$model);
210 $partialHelper = $this->view
->plugin('partial');
211 return $partialHelper($partial, $model);
215 * Sets whether last page in breadcrumbs should be hyperlinked
217 * @param bool $linkLast whether last page should be hyperlinked
218 * @return Breadcrumbs
220 public function setLinkLast($linkLast)
222 $this->linkLast
= (bool) $linkLast;
227 * Returns whether last page in breadcrumbs should be hyperlinked
231 public function getLinkLast()
233 return $this->linkLast
;
237 * Sets which partial view script to use for rendering menu
239 * @param string|array $partial partial view script or null. If an array is
240 * given, it is expected to contain two
241 * values; the partial view script to use,
242 * and the module where the script can be
244 * @return Breadcrumbs
246 public function setPartial($partial)
248 if (null === $partial ||
is_string($partial) ||
is_array($partial)) {
249 $this->partial
= $partial;
256 * Returns partial view script to use for rendering menu
258 * @return string|array|null
260 public function getPartial()
262 return $this->partial
;
266 * Sets breadcrumb separator
268 * @param string $separator separator string
269 * @return Breadcrumbs
271 public function setSeparator($separator)
273 if (is_string($separator)) {
274 $this->separator
= $separator;
281 * Returns breadcrumb separator
283 * @return string breadcrumb separator
285 public function getSeparator()
287 return $this->separator
;