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

Commit

Permalink
Merge pull request #5358 from froschdesign/nav-helper/extract-transla…
Browse files Browse the repository at this point in the history
…tion

[Zend\Navigation] Extracting the translation from "htmlify"-method into ...
  • Loading branch information
weierophinney committed Oct 30, 2013
2 parents b7dd365 + b3b79f0 commit 0123eef
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 65 deletions.
51 changes: 33 additions & 18 deletions library/Zend/View/Helper/Navigation/AbstractHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,12 @@ public function findActive($container, $minDepth = null, $maxDepth = -1)

$found = null;
$foundDepth = -1;
$iterator = new RecursiveIteratorIterator($container, RecursiveIteratorIterator::CHILD_FIRST);
$iterator = new RecursiveIteratorIterator(
$container,
RecursiveIteratorIterator::CHILD_FIRST
);

/** @var \Zend\Navigation\Page\AbstractPage $page */
foreach ($iterator as $page) {
$currDepth = $iterator->getDepth();
if ($currDepth < $minDepth || !$this->accept($page)) {
Expand Down Expand Up @@ -388,25 +392,12 @@ protected function htmlAttribs($attribs)
* Returns an HTML string containing an 'a' element for the given page
*
* @param AbstractPage $page page to generate HTML for
* @return string
* @return string HTML string (<a href="…">Label</a>)
*/
public function htmlify(AbstractPage $page)
{
// get label and title for translating
$label = $page->getLabel();
$title = $page->getTitle();

if (null !== ($translator = $this->getTranslator())) {
if (null === ($textDomain = $page->getTextDomain())) {
$textDomain = $this->getTranslatorTextDomain();
}
if (is_string($label) && !empty($label)) {
$label = $translator->translate($label, $textDomain);
}
if (is_string($title) && !empty($title)) {
$title = $translator->translate($title, $textDomain);
}
}
$label = $this->translate($page->getLabel(), $page->getTextDomain());
$title = $this->translate($page->getTitle(), $page->getTextDomain());

// get attribs for anchor element
$attribs = array(
Expand All @@ -417,9 +408,33 @@ public function htmlify(AbstractPage $page)
'target' => $page->getTarget()
);

/** @var \Zend\View\Helper\EscapeHtml $escaper */
$escaper = $this->view->plugin('escapeHtml');
$label = $escaper($label);

return '<a' . $this->htmlAttribs($attribs) . '>' . $label . '</a>';
}

/**
* Translate a message (for label, title, …)
*
* @param string $message ID of the message to translate
* @param string $textDomain Text domain (category name for the translations)
* @return string Translated message
*/
protected function translate($message, $textDomain = null)
{
if (is_string($message) && !empty($message)) {
if (null !== ($translator = $this->getTranslator())) {
if (null === $textDomain) {
$textDomain = $this->getTranslatorTextDomain();
}

return $translator->translate($message, $textDomain);
}
}

return '<a' . $this->htmlAttribs($attribs) . '>' . $escaper($label) . '</a>';
return $message;
}

/**
Expand Down
19 changes: 8 additions & 11 deletions library/Zend/View/Helper/Navigation/Breadcrumbs.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,11 @@ public function renderStraight($container = null)
if ($this->getLinkLast()) {
$html = $this->htmlify($active);
} else {
$html = $active->getLabel();
if (null !== ($translator = $this->getTranslator())) {
if (null === ($textDomain = $active->getTextDomain())) {
$textDomain = $this->getTranslatorTextDomain();
}
$html = $translator->translate($html, $textDomain);
}
/** @var \Zend\View\Helper\EscapeHtml $escaper */
$escaper = $this->view->plugin('escapeHtml');
$html = $escaper($html);
$html = $escaper(
$this->translate($active->getLabel(), $active->getTextDomain())
);
}

// walk back to root
Expand Down Expand Up @@ -200,6 +196,9 @@ public function renderPartial($container = null, $partial = null)
$model['pages'] = array_reverse($model['pages']);
}

/** @var \Zend\View\Helper\Partial $partialHelper */
$partialHelper = $this->view->plugin('partial');

if (is_array($partial)) {
if (count($partial) != 2) {
throw new Exception\InvalidArgumentException(
Expand All @@ -209,11 +208,9 @@ public function renderPartial($container = null, $partial = null)
);
}

$partialHelper = $this->view->plugin('partial');
return $partialHelper($partial[0], /*$partial[1], */$model);
return $partialHelper($partial[0], $model);
}

$partialHelper = $this->view->plugin('partial');
return $partialHelper($partial, $model);
}

Expand Down
58 changes: 22 additions & 36 deletions library/Zend/View/Helper/Navigation/Menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ protected function renderDeepestMenu(
}

$ulClass = $ulClass ? ' class="' . $ulClass . '"' : '';
$html = $indent . '<ul' . $ulClass . '>' . self::EOL;
$html = $indent . '<ul' . $ulClass . '>' . PHP_EOL;

foreach ($active['page'] as $subPage) {
if (!$this->accept($subPage)) {
Expand All @@ -163,9 +163,9 @@ protected function renderDeepestMenu(
}
$liClass = empty($liClasses) ? '' : ' class="' . implode(' ', $liClasses) . '"';

$html .= $indent . ' <li' . $liClass . '>' . self::EOL;
$html .= $indent . ' ' . $this->htmlify($subPage, $escapeLabels, $addClassToListItem) . self::EOL;
$html .= $indent . ' </li>' . self::EOL;
$html .= $indent . ' <li' . $liClass . '>' . PHP_EOL;
$html .= $indent . ' ' . $this->htmlify($subPage, $escapeLabels, $addClassToListItem) . PHP_EOL;
$html .= $indent . ' </li>' . PHP_EOL;
}

$html .= $indent . '</ul>';
Expand Down Expand Up @@ -305,19 +305,19 @@ protected function renderNormalMenu(
} else {
$ulClass = '';
}
$html .= $myIndent . '<ul' . $ulClass . '>' . self::EOL;
$html .= $myIndent . '<ul' . $ulClass . '>' . PHP_EOL;
} elseif ($prevDepth > $depth) {
// close li/ul tags until we're at current depth
for ($i = $prevDepth; $i > $depth; $i--) {
$ind = $indent . str_repeat(' ', $i);
$html .= $ind . ' </li>' . self::EOL;
$html .= $ind . '</ul>' . self::EOL;
$html .= $ind . ' </li>' . PHP_EOL;
$html .= $ind . '</ul>' . PHP_EOL;
}
// close previous li tag
$html .= $myIndent . ' </li>' . self::EOL;
$html .= $myIndent . ' </li>' . PHP_EOL;
} else {
// close previous li tag
$html .= $myIndent . ' </li>' . self::EOL;
$html .= $myIndent . ' </li>' . PHP_EOL;
}

// render li tag and page
Expand All @@ -332,8 +332,8 @@ protected function renderNormalMenu(
}
$liClass = empty($liClasses) ? '' : ' class="' . implode(' ', $liClasses) . '"';

$html .= $myIndent . ' <li' . $liClass . '>' . self::EOL
. $myIndent . ' ' . $this->htmlify($page, $escapeLabels, $addClassToListItem) . self::EOL;
$html .= $myIndent . ' <li' . $liClass . '>' . PHP_EOL
. $myIndent . ' ' . $this->htmlify($page, $escapeLabels, $addClassToListItem) . PHP_EOL;

// store as previous depth for next iteration
$prevDepth = $depth;
Expand All @@ -343,10 +343,10 @@ protected function renderNormalMenu(
// done iterating container; close open ul/li tags
for ($i = $prevDepth+1; $i > 0; $i--) {
$myIndent = $indent . str_repeat(' ', $i-1);
$html .= $myIndent . ' </li>' . self::EOL
. $myIndent . '</ul>' . self::EOL;
$html .= $myIndent . ' </li>' . PHP_EOL
. $myIndent . '</ul>' . PHP_EOL;
}
$html = rtrim($html, self::EOL);
$html = rtrim($html, PHP_EOL);
}

return $html;
Expand Down Expand Up @@ -394,6 +394,9 @@ public function renderPartial($container = null, $partial = null)
'container' => $container
);

/** @var \Zend\View\Helper\Partial $partialHelper */
$partialHelper = $this->view->plugin('partial');

if (is_array($partial)) {
if (count($partial) != 2) {
throw new Exception\InvalidArgumentException(
Expand All @@ -403,11 +406,9 @@ public function renderPartial($container = null, $partial = null)
);
}

$partialHelper = $this->view->plugin('partial');
return $partialHelper($partial[0], /*$partial[1], */$model);
return $partialHelper($partial[0], $model);
}

$partialHelper = $this->view->plugin('partial');
return $partialHelper($partial, $model);
}

Expand Down Expand Up @@ -471,27 +472,10 @@ public function renderSubMenu(
*/
public function htmlify(AbstractPage $page, $escapeLabel = true, $addClassToListItem = false)
{
// get label and title for translating
$label = $page->getLabel();
$title = $page->getTitle();

// translate label and title?
if (null !== ($translator = $this->getTranslator())) {
if (null === ($textDomain = $page->getTextDomain())) {
$textDomain = $this->getTranslatorTextDomain();
}
if (is_string($label) && !empty($label)) {
$label = $translator->translate($label, $textDomain);
}
if (is_string($title) && !empty($title)) {
$title = $translator->translate($title, $textDomain);
}
}

// get attribs for element
$attribs = array(
'id' => $page->getId(),
'title' => $title,
'title' => $this->translate($page->getTitle(), $page->getTextDomain()),
);

if ($addClassToListItem === false) {
Expand All @@ -508,8 +492,10 @@ public function htmlify(AbstractPage $page, $escapeLabel = true, $addClassToList
$element = 'span';
}

$html = '<' . $element . $this->htmlAttribs($attribs) . '>';
$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 {
Expand Down

0 comments on commit 0123eef

Please sign in to comment.