isListed() – missing something?

Hi there,

maybe it’s because I haven’t slept much, but I can’t get this to work:

<?php if($site->children()->find('contact')->isListed()): ?>
    <some html here>
<?php endif; ?>

It throws “Call to a member function isListed() on null”.
I thought it would work like isNotEmpty().

Thx!

Are you sure that there is a contact page? Usually that message is down to something missing or not the right type.

isListed() checks if the page has status listed. You can only use such a method if the object actually exists. So the correct code would be

<?php if(($page = $site->children()->find('contact')) && $page->isListed()): ?>
    <some html here>
<?php endif; ?>

isNotEmpty() is a collection method. Usually, a collection like $page->children() exists no matter if it has elements or not.

2 Likes

Thanks for the correction of the code!

Maybe I’m misunderstanding something but from my understanding doesn’t the ($page = $site->children()->find(‘contact’)) check whether a page exists no matter its status? If it only checks pages which are published, what’s the difference between that and isListed().

Because a page can only have the status “listed” if it’s existing anyways, no?

In your first example, you use an if statement but you already add the isListed() method to the chain without first checking if you have a page object.

The thing to understand here is that class methods require an existing instance of that class.

A published page can have two statuses: either listed or unlisted. An unpublished page is a draft.

1 Like

Ah ok, so I misunderstood the difference between a draft and a listed/unlisted status.
Because I thought a status “unlisted” actually means the page is a draft. What’s the difference then (sorry for asking).

Apparently I overlooked the isDraft(). So from my understanding isDraft() needs a page existence check too.

A listed page is a page with any kind of sorting number, either manual like 1_xxx, or 0_xxx for alphabetical sorting or 20191211_ for date based sorting. An unlisted page doesn’t have a sorting number but is still available via its URL. A draft is only visible for logged in users.

https://getkirby.com/docs/guide/content/publishing-workflow#page-states

1 Like

As I said, any method needs an existing object (apart from static methods… like Str::upper())

Ah, cool, yeah I couldn’t find the link in the search results.

One last question now: :smiley:

In what case would somebody need a page – which is not ready for the general public – to be accessible via URL?

It’s just a flag, I often use them for legal pages etc.

1 Like

Ok, thanks for explaining!