Skip to content
This repository was archived by the owner on Jan 31, 2020. It is now read-only.

Commit b44b831

Browse files
committed
Merge branch 'hotfix/8' into develop
Close #8
2 parents fbd1356 + a092a1d commit b44b831

File tree

9 files changed

+359
-268
lines changed

9 files changed

+359
-268
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ All notable changes to this project will be documented in this file, in reverse
66

77
### Added
88

9-
- Nothing.
9+
- [#8](https://github.com/zendframework/zend-view/pull/8) adds a new method to
10+
each of the `Breadcrumbs` and `Menu` navigation helpers,
11+
`renderPartialWithParams(array $params = [], $container = null, $partial = null)`.
12+
This method allows passing parameters to the navigation partial to render,
13+
just as you would when using the `partial()` view helper.
1014

1115
### Deprecated
1216

src/Helper/Navigation/Breadcrumbs.php

Lines changed: 114 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -15,40 +15,40 @@
1515
use Zend\View\Exception;
1616

1717
/**
18-
* Helper for printing breadcrumbs
18+
* Helper for printing breadcrumbs.
1919
*/
2020
class Breadcrumbs extends AbstractHelper
2121
{
2222
/**
23-
* Whether last page in breadcrumb should be hyperlinked
23+
* Whether last page in breadcrumb should be hyperlinked.
2424
*
2525
* @var bool
2626
*/
2727
protected $linkLast = false;
2828

2929
/**
30-
* The minimum depth a page must have to be included when rendering
30+
* The minimum depth a page must have to be included when rendering.
3131
*
3232
* @var int
3333
*/
3434
protected $minDepth = 1;
3535

3636
/**
37-
* Partial view script to use for rendering menu
37+
* Partial view script to use for rendering menu.
3838
*
3939
* @var string|array
4040
*/
4141
protected $partial;
4242

4343
/**
44-
* Breadcrumbs separator string
44+
* Breadcrumbs separator string.
4545
*
4646
* @var string
4747
*/
4848
protected $separator = ' > ';
4949

5050
/**
51-
* Helper entry point
51+
* Helper entry point.
5252
*
5353
* @param string|AbstractContainer $container container to operate on
5454
* @return Breadcrumbs
@@ -63,7 +63,7 @@ public function __invoke($container = null)
6363
}
6464

6565
/**
66-
* Renders helper
66+
* Renders helper.
6767
*
6868
* Implements {@link HelperInterface::render()}.
6969
*
@@ -83,7 +83,7 @@ public function render($container = null)
8383

8484
/**
8585
* Renders breadcrumbs by chaining 'a' elements with the separator
86-
* registered in the helper
86+
* registered in the helper.
8787
*
8888
* @param AbstractContainer $container [optional] container to render. Default is
8989
* to render the container registered in the helper.
@@ -135,87 +135,53 @@ public function renderStraight($container = null)
135135
}
136136

137137
/**
138-
* Renders the given $container by invoking the partial view helper
138+
* Renders the given $container by invoking the partial view helper.
139139
*
140-
* The container will simply be passed on as a model to the view script,
141-
* so in the script it will be available in <code>$this->container</code>.
140+
* The container will simply be passed on as a model to the view script
141+
* as-is, and will be available in the partial script as 'container', e.g.
142+
* <code>echo 'Number of pages: ', count($this->container);</code>.
142143
*
143-
* @param AbstractContainer $container [optional] container to pass to view script.
144-
* Default is to use the container registered
145-
* in the helper.
146-
* @param string|array $partial [optional] partial view script to use.
147-
* Default is to use the partial registered
148-
* in the helper. If an array is given, it
149-
* is expected to contain two values; the
150-
* partial view script to use, and the module
151-
* where the script can be found.
152-
* @throws Exception\RuntimeException if no partial provided
144+
* @param null|AbstractContainer $container [optional] container to pass to view
145+
* script. Default is to use the container registered in the helper.
146+
* @param null|string|array $partial [optional] partial view script to use.
147+
* Default is to use the partial registered in the helper. If an array
148+
* is given, it is expected to contain two values; the partial view
149+
* script to use, and the module where the script can be found.
150+
* @return string
151+
* @throws Exception\RuntimeException if no partial provided
153152
* @throws Exception\InvalidArgumentException if partial is invalid array
154-
* @return string helper output
155153
*/
156154
public function renderPartial($container = null, $partial = null)
157155
{
158-
$this->parseContainer($container);
159-
if (null === $container) {
160-
$container = $this->getContainer();
161-
}
162-
163-
if (null === $partial) {
164-
$partial = $this->getPartial();
165-
}
166-
167-
if (empty($partial)) {
168-
throw new Exception\RuntimeException(
169-
'Unable to render menu: No partial view script provided'
170-
);
171-
}
172-
173-
// put breadcrumb pages in model
174-
$model = [
175-
'pages' => [],
176-
'separator' => $this->getSeparator()
177-
];
178-
$active = $this->findActive($container);
179-
if ($active) {
180-
$active = $active['page'];
181-
$model['pages'][] = $active;
182-
while ($parent = $active->getParent()) {
183-
if ($parent instanceof AbstractPage) {
184-
$model['pages'][] = $parent;
185-
} else {
186-
break;
187-
}
188-
189-
if ($parent === $container) {
190-
// break if at the root of the given container
191-
break;
192-
}
193-
194-
$active = $parent;
195-
}
196-
$model['pages'] = array_reverse($model['pages']);
197-
}
198-
199-
/** @var \Zend\View\Helper\Partial $partialHelper */
200-
$partialHelper = $this->view->plugin('partial');
201-
202-
if (is_array($partial)) {
203-
if (count($partial) != 2) {
204-
throw new Exception\InvalidArgumentException(
205-
'Unable to render menu: A view partial supplied as '
206-
. 'an array must contain two values: partial view '
207-
. 'script and module where script can be found'
208-
);
209-
}
210-
211-
return $partialHelper($partial[0], $model);
212-
}
156+
return $this->renderPartialModel([], $container, $partial);
157+
}
213158

214-
return $partialHelper($partial, $model);
159+
/**
160+
* Renders the given $container by invoking the partial view helper with the given parameters as the model.
161+
*
162+
* The container will simply be passed on as a model to the view script
163+
* as-is, and will be available in the partial script as 'container', e.g.
164+
* <code>echo 'Number of pages: ', count($this->container);</code>.
165+
*
166+
* Any parameters provided will be passed to the partial via the view model.
167+
*
168+
* @param null|AbstractContainer $container [optional] container to pass to view
169+
* script. Default is to use the container registered in the helper.
170+
* @param null|string|array $partial [optional] partial view script to use.
171+
* Default is to use the partial registered in the helper. If an array
172+
* is given, it is expected to contain two values; the partial view
173+
* script to use, and the module where the script can be found.
174+
* @return string
175+
* @throws Exception\RuntimeException if no partial provided
176+
* @throws Exception\InvalidArgumentException if partial is invalid array
177+
*/
178+
public function renderPartialWithParams(array $params = [], $container = null, $partial = null)
179+
{
180+
return $this->renderPartialModel($params, $container, $partial);
215181
}
216182

217183
/**
218-
* Sets whether last page in breadcrumbs should be hyperlinked
184+
* Sets whether last page in breadcrumbs should be hyperlinked.
219185
*
220186
* @param bool $linkLast whether last page should be hyperlinked
221187
* @return Breadcrumbs
@@ -227,7 +193,7 @@ public function setLinkLast($linkLast)
227193
}
228194

229195
/**
230-
* Returns whether last page in breadcrumbs should be hyperlinked
196+
* Returns whether last page in breadcrumbs should be hyperlinked.
231197
*
232198
* @return bool
233199
*/
@@ -237,26 +203,23 @@ public function getLinkLast()
237203
}
238204

239205
/**
240-
* Sets which partial view script to use for rendering menu
206+
* Sets which partial view script to use for rendering menu.
241207
*
242208
* @param string|array $partial partial view script or null. If an array is
243-
* given, it is expected to contain two
244-
* values; the partial view script to use,
245-
* and the module where the script can be
246-
* found.
209+
* given, it is expected to contain two values; the partial view script
210+
* to use, and the module where the script can be found.
247211
* @return Breadcrumbs
248212
*/
249213
public function setPartial($partial)
250214
{
251215
if (null === $partial || is_string($partial) || is_array($partial)) {
252216
$this->partial = $partial;
253217
}
254-
255218
return $this;
256219
}
257220

258221
/**
259-
* Returns partial view script to use for rendering menu
222+
* Returns partial view script to use for rendering menu.
260223
*
261224
* @return string|array|null
262225
*/
@@ -266,7 +229,7 @@ public function getPartial()
266229
}
267230

268231
/**
269-
* Sets breadcrumb separator
232+
* Sets breadcrumb separator.
270233
*
271234
* @param string $separator separator string
272235
* @return Breadcrumbs
@@ -281,12 +244,73 @@ public function setSeparator($separator)
281244
}
282245

283246
/**
284-
* Returns breadcrumb separator
247+
* Returns breadcrumb separator.
285248
*
286-
* @return string breadcrumb separator
249+
* @return string breadcrumb separator
287250
*/
288251
public function getSeparator()
289252
{
290253
return $this->separator;
291254
}
255+
256+
/**
257+
* Render a partial with the given "model".
258+
*
259+
* @param array $params
260+
* @param null|AbstractContainer $container
261+
* @param null|string|array $partial
262+
* @return string
263+
* @throws Exception\RuntimeException if no partial provided
264+
* @throws Exception\InvalidArgumentException if partial is invalid array
265+
*/
266+
protected function renderPartialModel(array $params, $container, $partial)
267+
{
268+
$this->parseContainer($container);
269+
if (null === $container) {
270+
$container = $this->getContainer();
271+
}
272+
if (null === $partial) {
273+
$partial = $this->getPartial();
274+
}
275+
if (empty($partial)) {
276+
throw new Exception\RuntimeException(
277+
'Unable to render breadcrumbs: No partial view script provided'
278+
);
279+
}
280+
$model = array_merge($params, ['pages' => []], ['separator' => $this->getSeparator()]);
281+
$active = $this->findActive($container);
282+
if ($active) {
283+
$active = $active['page'];
284+
$model['pages'][] = $active;
285+
while ($parent = $active->getParent()) {
286+
if (! $parent instanceof AbstractPage) {
287+
break;
288+
}
289+
290+
$model['pages'][] = $parent;
291+
if ($parent === $container) {
292+
// break if at the root of the given container
293+
break;
294+
}
295+
$active = $parent;
296+
}
297+
$model['pages'] = array_reverse($model['pages']);
298+
}
299+
300+
/** @var \Zend\View\Helper\Partial $partialHelper */
301+
$partialHelper = $this->view->plugin('partial');
302+
if (is_array($partial)) {
303+
if (count($partial) != 2) {
304+
throw new Exception\InvalidArgumentException(
305+
'Unable to render breadcrumbs: A view partial supplied as '
306+
. 'an array must contain two values: partial view '
307+
. 'script and module where script can be found'
308+
);
309+
}
310+
311+
return $partialHelper($partial[0], $model);
312+
}
313+
314+
return $partialHelper($partial, $model);
315+
}
292316
}

0 commit comments

Comments
 (0)