Skip to content

Commit

Permalink
Simplify and fix breadcrumb builder
Browse files Browse the repository at this point in the history
  • Loading branch information
billtomczak committed Jul 14, 2022
1 parent 9e8b90f commit c1f3ccb
Showing 1 changed file with 29 additions and 18 deletions.
47 changes: 29 additions & 18 deletions src/admin/library/Free/Helper/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,33 +43,44 @@ class View
*/
public function buildCategoryBreadcrumbs($categoryId)
{
$app = Factory::getApplication();
$routing = Factory::getPimpleContainer()->helperRoute;
$pathway = $app->getPathway();
$itemId = $app->input->getInt('Itemid');
$pathwayItems = $pathway->getPathway();

$lastItem = [];
parse_str(
parse_url(
end($pathwayItems)->link,
PHP_URL_QUERY
),
$lastItem
);
if ($lastItem['id'] == $categoryId && $lastItem['Itemid'] == $itemId) {
// We're already on the required menu
return;
}

$paths = [];
$this->buildPath($paths, $categoryId);

$app = Factory::getApplication();
$container = Factory::getPimpleContainer();

$pathway = $app->getPathway();
$itemId = $app->input->getInt('Itemid');
$countPaths = count($paths) - 1;

$pathwayList = $pathway->getPathway();

for ($i = $countPaths; $i >= 0; $i--) {
$link = $container->helperRoute->getFileListRoute($paths[$i]->id, $itemId);
$paths = array_reverse($paths);
foreach ($paths as $path) {
$link = $routing->getFileListRoute($path->id, $itemId);
$route = Route::_($link);
$exists = false;

// Check if the current category is already in the pathway, to ignore
foreach ($pathwayList as $item) {
if ($item->link === $link) {
foreach ($pathwayItems as $pathwayItem) {
if ($pathwayItem->link === $link) {
$exists = true;
break;
}
}

if (!$exists) {
$pathway->addItem($paths[$i]->title, $route);
$pathway->addItem($path->title, $route);
}
}
}
Expand All @@ -88,7 +99,7 @@ public function buildFileBreadcrumbs($file)

if ($activeMenu = $container->app->getMenu()->getActive()) {
$view = $activeMenu->query['view'] ?? null;
$id = $activeMenu->query['id'] ?? null;
$id = $activeMenu->query['id'] ?? null;
if ($view == 'item' && $id == $file->id) {
// Current menu is for this file
return;
Expand All @@ -111,8 +122,8 @@ public function buildFileBreadcrumbs($file)
/**
* Build an inverse recurcive list of paths for categories' breadcrumbs.
*
* @param array &$paths
* @param int $categoryId
* @param object[] &$paths
* @param int $categoryId
*/
protected function buildPath(&$paths, $categoryId)
{
Expand All @@ -131,7 +142,7 @@ protected function buildPath(&$paths, $categoryId)
]);
$category = $db->setQuery($query)->loadObject();

if (!empty($category)) {
if ($category) {
$paths[] = $category;

if ($category->parent_id) {
Expand Down

0 comments on commit c1f3ccb

Please sign in to comment.