Skip to content

Commit

Permalink
FIX bookmarks with unpubilshed parents breaking site
Browse files Browse the repository at this point in the history
When an ancestor page is unpublished, but a child page with a bookmark
on it remains published, the member to whom this bookmark belongs
receives a 500 error for any page on the site.

This is due to PHP 8 increasing warning errors to fatal errors with type
expectation mismatches - in this case `false` being passed to `count()`
which expects an array.

Now with a failing test and a more comprehensive test suite we can have
confidence that this is both fixed, and did not introduce another error
by inadvertently altering behaviour.
  • Loading branch information
NightJar committed Dec 22, 2022
1 parent a9b092a commit b83c6e6
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions src/extensions/BookmarksMemberExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 [];
}

/**
Expand Down

0 comments on commit b83c6e6

Please sign in to comment.