Hauptkategorie im Modul ermitteln
Quell-ID: GitHub Discussion #37
Use Case
In einem Modul soll die aktuell gewählte Hauptkategorie (Root-Kategorie) ermittelt werden, um auf deren Metadaten zugreifen zu können.
Verwendete AddOns
- REDAXO Core
Problemstellung
Bei verschachtelten Kategorien soll unabhängig von der aktuellen Tiefe die oberste Kategorie im Pfad ermittelt werden.
Lösung
Mit getPathAsArray() kann der Kategorie-Pfad des aktuellen Artikels abgerufen werden:
<?php
$currentPath = rex_article::getCurrent()->getPathAsArray();
$rootCategory = rex_category::get($currentPath[0]);
// Metadaten der Hauptkategorie abrufen
echo $rootCategory->getValue('cat_meta_image');
echo $rootCategory->getValue('cat_color');
Erklärung
getPathAsArray() liefert ein Array aller übergeordneten Kategorie-IDs, beginnend mit der Root-Kategorie:
Beispiel-Struktur:
- Kategorie 5 (Root)
- Kategorie 12
- Kategorie 23 (aktuell)
$path = [5, 12, 23]
$path[0] = 5 (Root-Kategorie)
Weitere Beispiele
Bestimmte Ebene ermitteln
<?php
$path = rex_article::getCurrent()->getPathAsArray();
// Root-Kategorie (Ebene 1)
$level1 = isset($path[0]) ? rex_category::get($path[0]) : null;
// Zweite Ebene
$level2 = isset($path[1]) ? rex_category::get($path[1]) : null;
// Dritte Ebene
$level3 = isset($path[2]) ? rex_category::get($path[2]) : null;
Prüfen ob Root-Kategorie
<?php
$article = rex_article::getCurrent();
$path = $article->getPathAsArray();
if (count($path) === 1 || $article->getCategoryId() === $path[0]) {
// Wir sind in der Root-Kategorie
echo "Dies ist eine Hauptkategorie";
}
Navigation basierend auf Hauptkategorie
<?php
$path = rex_article::getCurrent()->getPathAsArray();
$rootCatId = $path[0] ?? 0;
// Styling basierend auf Hauptkategorie
$themes = [
5 => 'blue',
8 => 'green',
12 => 'orange',
];
$theme = $themes[$rootCatId] ?? 'default';
echo '<body class="theme-' . $theme . '">';
Besserer Ansatz
Als Helper-Klasse
<?php
class CategoryHelper
{
/**
* Gibt die Root-Kategorie des aktuellen Artikels zurück
*/
public static function getRootCategory(): ?rex_category
{
$article = rex_article::getCurrent();
if (!$article) {
return null;
}
$path = $article->getPathAsArray();
if (empty($path)) {
// Artikel ohne Kategorie
return null;
}
return rex_category::get($path[0]);
}
/**
* Gibt eine Kategorie auf der angegebenen Ebene zurück
* @param int $level 1 = Root, 2 = zweite Ebene, etc.
*/
public static function getCategoryByLevel(int $level): ?rex_category
{
$article = rex_article::getCurrent();
if (!$article) {
return null;
}
$path = $article->getPathAsArray();
$index = $level - 1;
if (!isset($path[$index])) {
return null;
}
return rex_category::get($path[$index]);
}
/**
* Gibt alle Kategorien im Pfad zurück
* @return rex_category[]
*/
public static function getBreadcrumbCategories(): array
{
$article = rex_article::getCurrent();
if (!$article) {
return [];
}
$categories = [];
foreach ($article->getPathAsArray() as $catId) {
$cat = rex_category::get($catId);
if ($cat) {
$categories[] = $cat;
}
}
return $categories;
}
}
// Verwendung im Modul
$rootCat = CategoryHelper::getRootCategory();
if ($rootCat) {
echo 'Hauptbereich: ' . $rootCat->getName();
echo 'Bild: ' . $rootCat->getValue('cat_image');
}
Caching bei häufiger Nutzung
<?php
// In boot.php für globalen Zugriff
if (rex::isFrontend()) {
$path = rex_article::getCurrent()?->getPathAsArray() ?? [];
$rootCat = !empty($path) ? rex_category::get($path[0]) : null;
rex::setProperty('root_category', $rootCat);
}
// Später im Modul
$rootCat = rex::getProperty('root_category');