Jump to content

How can i check if current page's children are viewable?


doolak
 Share

Recommended Posts

Hi there,

well - the title probably describes enough what i want to ask...

I am working on a navigation and i want to avoid that a sublevel <ul></ul> - tag is generated when the children of that page is not viewable - so i want to verify if they are or not.

I tried:

if ($page->children->viewable()) $out = "\n<ul>";

... but this does not work - seems that this is just possible for pages with $page->viewable()

Would be great if you could help me.

Cheers,

Christian

Link to comment
Share on other sites

You can check if a viewable child (or more) exists like this:

$viewableChildren = false;
foreach($page->children() as $child) {
 if($child->viewable()) {
   $viewableChildren = true;
   break;
 }
}

if($viewableChildren) {
 // ...
}
Link to comment
Share on other sites

If there aren't any viewable children, then count($page->children) and $page->numChildren will be 0. Keep in mind that ProcessWire doesn't include unviewable pages in $page->children unless you add "include=all" or "check_access=0" to your selector. So there's really no reason to cycle through the pages and check if they are viewable unless they aren't viewable due to lack of a template file. Also keep in mind that a page is viewable or not viewable within the context of a user. So just because they are viewable to you doesn't mean they are viewable to an anonymous user browsing your site. So if you want to see how it will look to other users, logout and then view the page (or view it in another browser where you aren't logged in).

Link to comment
Share on other sites

Thank you very much for your answers!

I made a error in reasoning - the children are hidden by the navigation template:

$children = $rootPage->children('template!=news_article');

So of course it does not work to exclude them the way i tried with count($page->children), $page->numChildren or if($child->viewable()).

I used the navigation snippet which was discussed already in another thread:

http://processwire.c...e__hl__treemenu

I took part asking how to hide the children - so i should not have made the error in reasoning now... :unsure:

Now i discovered that there is the module "MarkupSimpleNavigation" from Soma (thanks, Soma!) - works great and makes a lot of variations easier than digging into the code.

Thanks again to everybody!

  • Like 2
Link to comment
Share on other sites

  • 3 years later...

How is it possible to check if a page is viewable to specific role (guest)? Need this to build a proper sitemap.xml.

Hi, as the user can have multiple roles, I think you should not check it against a specific role.

For dynamic sitemap, $page->viewable() should do it. Telling you if the current user is able to see this page.

Or using $user->hasPermission($permission, $page) in case you want to check it from a user other then the current one.

Edit: In case you want to do the whole selection of pages from a specific users perspective, I think this should work (untested):

$currentUser = $user;
$tempUser = $users->get('guest');
$users->setCurrentUser($tempUser); 

//do your page selection

$users->setCurrentUser($currentUser); 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...