diff --git a/src/extensions/BookmarksMemberExtension.php b/src/extensions/BookmarksMemberExtension.php index 615b7a5..c6a413d 100755 --- a/src/extensions/BookmarksMemberExtension.php +++ b/src/extensions/BookmarksMemberExtension.php @@ -76,7 +76,7 @@ private function appendSiteTreeBookmark($link, $bookmarks) $category = $this->getTopLevelParent($siteTree); } - if (count($category) > 0) { + if ($category) { $key = key($category); $bookmarks = $this->addCategoryToBookmarks($key, $bookmarks, $category); @@ -118,7 +118,7 @@ private function appendUrlBookmark($link, $bookmarks) if ($url !== false) { // Remove first and last '/' characters from url path $paths = explode('/', trim($url['path'], '/')); - if (count($paths) > 0) { + if ($paths) { // Get the first part of a url as a title/key // e.g https://test.org/abc/test-page // use abc as a key and as a title for parent/category @@ -170,19 +170,15 @@ private function addCategoryToBookmarks($key, $bookmarks, $category) */ private function getTopLevelParent($siteTree) { - $siteTreeParent = SiteTree::get()->filter('ID', $siteTree->ParentID)->first(); + $ancestry = $siteTree->getAncestors(); - if ($siteTreeParent) { - if ($siteTreeParent->ParentID == 0) { - $parentKey = $this->getParentSiteTreeKey($siteTreeParent); - $parent[$parentKey]['Title'] = $siteTreeParent->Title; - return $parent; - } - - return $this->getTopLevelParent($siteTreeParent); + if ($ancestry->exists()) { + $topLevelParent = $ancestry->pop(); + $parentKey = $this->getParentSiteTreeKey($topLevelParent); + return [$parentKey => ['Title' => $topLevelParent->Title]]; } - return false; + return []; } /**