statamic / cms

The core Laravel CMS Composer package
https://statamic.com
Other
3.71k stars 508 forks source link

The "home" page is the parent of pages but has no children #4121

Closed rrelmy closed 8 months ago

rrelmy commented 3 years ago

Bug Description

Hard to describe as I am not sure if it is a real bug or it is just confusing behaviour. And it is most likely not possible to change as it would be a breaking change.

I call the root page a confused parent as it is defined as parent of other pages but itself has no children. While working with the page structure we were greeted with this unexpected behaviour.

How to Reproduce

Starting from an empty page

Extra Detail

/** @var \Statamic\Structures\Page $home */
$home = \Statamic\Facades\Entry::findByUri('/');
assert($home, 'Home exists');
assert(!$home->parent(), 'Home has no parent');

/** @var \Statamic\Structures\Page $faq */
$faq = \Statamic\Facades\Entry::findByUri('/faqs');
assert($faq->parent()->id() === $home->id(), 'Parent of FAQ page is home');

// check if home has children
assert($home->pages()->all()->count() >= 1, 'Home has at least one children'); # BOOOM 💣 

Environment

Statamic version: 3.1.31

jasonvarga commented 3 years ago

Thank you for pointing this out, but what is the real problem this is causing for you?

rrelmy commented 3 years ago

As a workaround for https://github.com/statamic/ideas/issues/622 we started implementing our own tag finding the next/previous. In a very similar way as the CollectionTags in v2 does.

Get all sibling pages by going ->parent()->pages() and find the current page and take the next/previous one.

In the case of the example with the FAQ page parent is home and we get no pages, as a workaround we need to check for the home and take all the top-level pages instead.

Pseudocode

function getSiblingPages($page) {
  if (isHome($page->parent())) {
    return findAllTopLevelPages();
  }

  return $page->parent()->pages()->all()
}
duncanmcclean commented 8 months ago

Closing - this is working as intended.