Look back checking multiple leves stopping if parent = site()

So what I have is a parent template. With various access to a feature. It will show or hide certain elements of the site based on permission. Here’s the issue. My current logic won’t work with children. Because if the ending template parent doesn’t have permission the children shouldn’t be able to either. for instance.

localhost:8888/faq/more/something/else
1 = checked
0 = not checked
if faq = 0
more = 1
something = 1
else=1

I run this check if $parent() == $site()

So what I want to do is escape all the way back until I get to the page right before $site() if at all along the way I hit a 0 I want to go into an else statement. But I want it to keep counting until it hits the page right before site which is FAQ. This way I can check all the way down a geneology to make sure the Parent closest to $site() has checked box. But I don’t want to check the $site->() template. Hope that makes sense.

<?php if ($page->accessToResourceCenter() == '1'): ?>
            <?php snippet('sidemenu') ?>
            <? else : ?>
            <?if($page->parent()->url() == $site->url()) : ?>

            <? else :?>
                <?php snippet('sidemenu') ?>
            <?endif?>

        <?php endif ?>

In this case none of the children would work because the parent which is next to site->() is checked faq=0 thus all the children could not have that permission to access the feature until parent was set to 0. In case too. That along it’s journey to find it’s parent next to $site-> it stumbles upon a 0 it would also then deny the permission.

If it doesn’t matter where along the way the permission is unchecked:

  • use pluck() to get an array of all values of that particular field in all parents
$allowed = $page->parents()->pluck('accessToResourceCenter', ','); 

Then you could check the value of the current page and all parents:

if($page->accessToResourceCenter() == '1' && !in_array('0', $allowed)) {
  // do something
} else {
  // do something else
}

If you only want to check the last parent:

if($page->parents()->last()->accessToResourceCenter() == '0') {
 // do something
}
```