Skip to content

Commit

Permalink
Compile navigation row via own method
Browse files Browse the repository at this point in the history
  • Loading branch information
rabauss committed Jan 3, 2020
1 parent 6b3340b commit 5caf6b2
Showing 1 changed file with 74 additions and 59 deletions.
133 changes: 74 additions & 59 deletions core-bundle/src/Resources/contao/modules/Module.php
Expand Up @@ -351,89 +351,104 @@ protected function renderNavigation($pid, $level=1, $host=null, $language=null)
break;
}

$row = $objSubpage->row();
$trail = \in_array($objSubpage->id, $objPage->trail);
$items[] = $this->compileNavigationRow($objPage, $objSubpage, $subitems, $href);
}
}

// Add classes first and last
if (!empty($items))
{
$last = \count($items) - 1;

// Use the path without query string to check for active pages (see #480)
list($path) = explode('?', Environment::get('request'), 2);
$items[0]['class'] = trim($items[0]['class'] . ' first');
$items[$last]['class'] = trim($items[$last]['class'] . ' last');
}

$objTemplate->items = $items;

return !empty($items) ? $objTemplate->parse() : '';
}

/**
* Compile the navigation row and return it as array
*
* @param PageModel $objPage
* @param PageModel $objSubpage
* @param string $subitems
* @param string $href
*
* @return array
*/
protected function compileNavigationRow($objPage, $objSubpage, $subitems, $href)
{
$row = $objSubpage->row();
$trail = \in_array($objSubpage->id, $objPage->trail);

// Use the path without query string to check for active pages (see #480)
list($path) = explode('?', Environment::get('request'), 2);

// Active page
if (($objPage->id == $objSubpage->id || ($objSubpage->type == 'forward' && $objPage->id == $objSubpage->jumpTo)) && !($this instanceof ModuleSitemap) && $href == $path)
{
// Mark active forward pages (see #4822)
$strClass = (($objSubpage->type == 'forward' && $objPage->id == $objSubpage->jumpTo) ? 'forward' . ($trail ? ' trail' : '') : 'active') . (($subitems != '') ? ' submenu' : '') . ($objSubpage->protected ? ' protected' : '') . ($objSubpage->cssClass ? ' ' . $objSubpage->cssClass : '');

$row['isActive'] = true;
$row['isTrail'] = false;
}
$row['isActive'] = true;
$row['isTrail'] = false;
}

// Regular page
else
{
$strClass = (($subitems != '') ? 'submenu' : '') . ($objSubpage->protected ? ' protected' : '') . ($trail ? ' trail' : '') . ($objSubpage->cssClass ? ' ' . $objSubpage->cssClass : '');

// Mark pages on the same level (see #2419)
if ($objSubpage->pid == $objPage->pid)
{
$strClass .= ' sibling';
}

$row['isActive'] = false;
$row['isTrail'] = $trail;
}

$row['subitems'] = $subitems;
$row['class'] = trim($strClass);
$row['title'] = StringUtil::specialchars($objSubpage->title, true);
$row['pageTitle'] = StringUtil::specialchars($objSubpage->pageTitle, true);
$row['link'] = $objSubpage->title;
$row['href'] = $href;
$row['rel'] = '';
$row['nofollow'] = (strncmp($objSubpage->robots, 'noindex,nofollow', 16) === 0); // backwards compatibility
$row['target'] = '';
$row['description'] = str_replace(array("\n", "\r"), array(' ', ''), $objSubpage->description);

// Override the link target
if ($objSubpage->type == 'redirect' && $objSubpage->target)
{
$row['target'] = ' target="_blank"';
}

$arrRel = array();
// Mark pages on the same level (see #2419)
if ($objSubpage->pid == $objPage->pid)
{
$strClass .= ' sibling';
}

if (strncmp($objSubpage->robots, 'noindex,nofollow', 16) === 0)
{
$arrRel[] = 'nofollow';
}
$row['isActive'] = false;
$row['isTrail'] = $trail;
}

if ($objSubpage->type == 'redirect' && $objSubpage->target)
{
$arrRel[] = 'noreferrer';
$arrRel[] = 'noopener';
}
$row['subitems'] = $subitems;
$row['class'] = trim($strClass);
$row['title'] = StringUtil::specialchars($objSubpage->title, true);
$row['pageTitle'] = StringUtil::specialchars($objSubpage->pageTitle, true);
$row['link'] = $objSubpage->title;
$row['href'] = $href;
$row['rel'] = '';
$row['nofollow'] = (strncmp($objSubpage->robots, 'noindex,nofollow', 16) === 0); // backwards compatibility
$row['target'] = '';
$row['description'] = str_replace(array("\n", "\r"), array(' ', ''), $objSubpage->description);

// Override the link target
if ($objSubpage->type == 'redirect' && $objSubpage->target)
{
$row['target'] = ' target="_blank"';
}

// Override the rel attribute
if (!empty($arrRel))
{
$row['rel'] = ' rel="' . implode(' ', $arrRel) . '"';
}
$arrRel = array();

$items[] = $row;
}
if (strncmp($objSubpage->robots, 'noindex,nofollow', 16) === 0)
{
$arrRel[] = 'nofollow';
}

// Add classes first and last
if (!empty($items))
if ($objSubpage->type == 'redirect' && $objSubpage->target)
{
$last = \count($items) - 1;

$items[0]['class'] = trim($items[0]['class'] . ' first');
$items[$last]['class'] = trim($items[$last]['class'] . ' last');
$arrRel[] = 'noreferrer';
$arrRel[] = 'noopener';
}

$objTemplate->items = $items;
// Override the rel attribute
if (!empty($arrRel))
{
$row['rel'] = ' rel="' . implode(' ', $arrRel) . '"';
}

return !empty($items) ? $objTemplate->parse() : '';
return $row;
}

/**
Expand Down

0 comments on commit 5caf6b2

Please sign in to comment.