1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
| <aside>
<ul class="list-unstyled ps-0">
<?php
$db = Factory::getContainer()->get('DatabaseDriver');
$query = $db->getQuery(true);
$query->select("`id`, `title`, `parent_id`, `alias`");
$query->from($db->quoteName("#__categories"));
$query->where($db->quoteName("extension") . ' LIKE "com_content"');
$query->order($db->quoteName("lft") . " ASC");
$db->setQuery($query);
$results = $db->loadObjectList();
$categories = [];
$root_category_id = 9; # set your own root category ID
if (count($results)) {
$temp_options = [];
foreach ($results as $item) {
array_push($temp_options, [
"catid" => $item->id,
"title" => $item->title,
"parent_id" => $item->parent_id,
"alias" => $item->alias,
]);
}
$parent_categories = [];
$child_categories = [];
foreach ($temp_options as $option) {
if ($option["parent_id"] == $root_category_id) {
$parent_categories[] = [
"catid" => $option["catid"],
"title" => $option["title"],
"alias" => $option["alias"],
"route" => Route::_(ContentHelperRoute::getCategoryRoute($option["catid"]))
];
} else {
$catid_match = array_search(
$option["parent_id"],
array_column($parent_categories, "catid")
);
if ($catid_match) {
$child_categories[$option["parent_id"]][] = [
"catid" => $option["catid"],
"title" => $option["title"],
"alias" => $option["alias"],
"route" => Route::_(ContentHelperRoute::getCategoryRoute($option["catid"]))
];
}
}
}
foreach($parent_categories as $p_cat) {
if(count($child_categories[$p_cat["catid"]]) > 0) {
/* parent category + toggler */
echo '<li class="mb-1">';
echo '<a' .
' class="btn d-flex w-100 btn-toggle align-items-center collapsed p-3"' .
' data-bs-toggle="collapse"' .
' data-bs-target="#catalog-collapse-'.$p_cat['catid'].'"' .
' aria-expanded="false"' .
' href="javascript:;"' .
'>' .
$p_cat["title"] .
' </a>';
echo '<div class="collapse" id="catalog-collapse-'.$p_cat['catid'].'">'.
'<ul class="btn-toggle-nav list-unstyled fw-normal pb-1 small">';
foreach($child_categories[$p_cat["catid"]] as $c_cat) {
echo '<li><a href="'.$c_cat["route"].'" class="btn d-flex w-100 align-items-center rounded collapsed p-3 m-0">' .
$c_cat['title'] .'</a></li>';
}
echo '</ul></div></li>';
} else {
echo '<li class="mb-1"><a class="btn d-flex w-100 align-items-center collapsed p-3 m-0" href="'.$p_cat["route"].'">'.
$p_cat["title"] . '</a></li>';
}
}
}
?>
</ul>
</aside>
|