element. * * @var bool */ protected $addClassToListItem = false; /** * Whether labels should be escaped. * * @var bool */ protected $escapeLabels = true; /** * Whether only active branch should be rendered. * * @var bool */ protected $onlyActiveBranch = false; /** * Partial view script to use for rendering menu. * * @var string|array */ protected $partial = null; /** * Whether parents should be rendered when only rendering active branch. * * @var bool */ protected $renderParents = true; /** * CSS class to use for the ul element. * * @var string */ protected $ulClass = 'navigation'; /** * CSS class to use for the active li element. * * @var string */ protected $liActiveClass = 'active'; /** * View helper entry point. * * Retrieves helper and optionally sets container to operate on. * * @param AbstractContainer $container [optional] container to operate on * @return self */ public function __invoke($container = null) { if (null !== $container) { $this->setContainer($container); } return $this; } /** * Renders menu. * * Implements {@link HelperInterface::render()}. * * If a partial view is registered in the helper, the menu will be rendered * using the given partial script. If no partial is registered, the menu * will be rendered as an 'ul' element by the helper's internal method. * * @see renderPartial() * @see renderMenu() * @param AbstractContainer $container [optional] container to render. Default is * to render the container registered in the helper. * @return string */ public function render($container = null) { $partial = $this->getPartial(); if ($partial) { return $this->renderPartial($container, $partial); } return $this->renderMenu($container); } /** * Renders the deepest active menu within [$minDepth, $maxDepth], (called from {@link renderMenu()}). * * @param AbstractContainer $container container to render * @param string $ulClass CSS class for first UL * @param string $indent initial indentation * @param int|null $minDepth minimum depth * @param int|null $maxDepth maximum depth * @param bool $escapeLabels Whether or not to escape the labels * @param bool $addClassToListItem Whether or not page class applied to
echo 'Number of pages: ', count($this->container);
.
*
* @param null|AbstractContainer $container [optional] container to pass to view
* script. Default is to use the container registered in the helper.
* @param null|string|array $partial [optional] partial view script to use.
* Default is to use the partial registered in the helper. If an array
* is given, the first value is used for the partial view script.
* @return string
* @throws Exception\RuntimeException if no partial provided
* @throws Exception\InvalidArgumentException if partial is invalid array
*/
public function renderPartial($container = null, $partial = null)
{
return $this->renderPartialModel([], $container, $partial);
}
/**
* Renders the given $container by invoking the partial view helper with the given parameters as the model.
*
* The container will simply be passed on as a model to the view script
* as-is, and will be available in the partial script as 'container', e.g.
* echo 'Number of pages: ', count($this->container);
.
*
* Any parameters provided will be passed to the partial via the view model.
*
* @param null|AbstractContainer $container [optional] container to pass to view
* script. Default is to use the container registered in the helper.
* @param null|string|array $partial [optional] partial view script to use.
* Default is to use the partial registered in the helper. If an array
* is given, the first value is used for the partial view script.
* @return string
* @throws Exception\RuntimeException if no partial provided
* @throws Exception\InvalidArgumentException if partial is invalid array
*/
public function renderPartialWithParams(array $params = [], $container = null, $partial = null)
{
return $this->renderPartialModel($params, $container, $partial);
}
/**
* Renders the inner-most sub menu for the active page in the $container.
*
* This is a convenience method which is equivalent to the following call:
*
* renderMenu($container, array(
* 'indent' => $indent,
* 'ulClass' => $ulClass,
* 'minDepth' => null,
* 'maxDepth' => null,
* 'onlyActiveBranch' => true,
* 'renderParents' => false,
* 'liActiveClass' => $liActiveClass
* ));
*
*
* @param AbstractContainer $container [optional] container to render.
* Default is to render the container registered in the helper.
* @param string $ulClass [optional] CSS class to use for UL element.
* Default is to use the value from {@link getUlClass()}.
* @param string|int $indent [optional] indentation as a string or number
* of spaces. Default is to use the value retrieved from
* {@link getIndent()}.
* @param string $liActiveClass [optional] CSS class to use for UL
* element. Default is to use the value from {@link getUlClass()}.
* @return string
*/
public function renderSubMenu(
AbstractContainer $container = null,
$ulClass = null,
$indent = null,
$liActiveClass = null
) {
return $this->renderMenu($container, [
'indent' => $indent,
'ulClass' => $ulClass,
'minDepth' => null,
'maxDepth' => null,
'onlyActiveBranch' => true,
'renderParents' => false,
'escapeLabels' => true,
'addClassToListItem' => false,
'liActiveClass' => $liActiveClass,
]);
}
/**
* Returns an HTML string containing an 'a' element for the given page if
* the page's href is not empty, and a 'span' element if it is empty.
*
* Overrides {@link AbstractHelper::htmlify()}.
*
* @param AbstractPage $page page to generate HTML for
* @param bool $escapeLabel Whether or not to escape the label
* @param bool $addClassToListItem Whether or not to add the page class to the list item
* @return string
*/
public function htmlify(AbstractPage $page, $escapeLabel = true, $addClassToListItem = false)
{
// get attribs for element
$attribs = [
'id' => $page->getId(),
'title' => $this->translate($page->getTitle(), $page->getTextDomain()),
];
if ($addClassToListItem === false) {
$attribs['class'] = $page->getClass();
}
// does page have a href?
$href = $page->getHref();
if ($href) {
$element = 'a';
$attribs['href'] = $href;
$attribs['target'] = $page->getTarget();
} else {
$element = 'span';
}
$html = '<' . $element . $this->htmlAttribs($attribs) . '>';
$label = $this->translate($page->getLabel(), $page->getTextDomain());
if ($escapeLabel === true) {
/** @var \Zend\View\Helper\EscapeHtml $escaper */
$escaper = $this->view->plugin('escapeHtml');
$html .= $escaper($label);
} else {
$html .= $label;
}
$html .= '' . $element . '>';
return $html;
}
/**
* Normalizes given render options.
*
* @param array $options [optional] options to normalize
* @return array
*/
protected function normalizeOptions(array $options = [])
{
if (isset($options['indent'])) {
$options['indent'] = $this->getWhitespace($options['indent']);
} else {
$options['indent'] = $this->getIndent();
}
if (isset($options['ulClass']) && $options['ulClass'] !== null) {
$options['ulClass'] = (string) $options['ulClass'];
} else {
$options['ulClass'] = $this->getUlClass();
}
if (array_key_exists('minDepth', $options)) {
if (null !== $options['minDepth']) {
$options['minDepth'] = (int) $options['minDepth'];
}
} else {
$options['minDepth'] = $this->getMinDepth();
}
if ($options['minDepth'] < 0 || $options['minDepth'] === null) {
$options['minDepth'] = 0;
}
if (array_key_exists('maxDepth', $options)) {
if (null !== $options['maxDepth']) {
$options['maxDepth'] = (int) $options['maxDepth'];
}
} else {
$options['maxDepth'] = $this->getMaxDepth();
}
if (! isset($options['onlyActiveBranch'])) {
$options['onlyActiveBranch'] = $this->getOnlyActiveBranch();
}
if (! isset($options['escapeLabels'])) {
$options['escapeLabels'] = $this->escapeLabels;
}
if (! isset($options['renderParents'])) {
$options['renderParents'] = $this->getRenderParents();
}
if (! isset($options['addClassToListItem'])) {
$options['addClassToListItem'] = $this->getAddClassToListItem();
}
if (isset($options['liActiveClass']) && $options['liActiveClass'] !== null) {
$options['liActiveClass'] = (string) $options['liActiveClass'];
} else {
$options['liActiveClass'] = $this->getLiActiveClass();
}
return $options;
}
/**
* Sets a flag indicating whether labels should be escaped.
*
* @param bool $flag [optional] escape labels
* @return self
*/
public function escapeLabels($flag = true)
{
$this->escapeLabels = (bool) $flag;
return $this;
}
/**
* Enables/disables page class applied to