Navigating Visible Siblings and Siblings Hidden Behind Authenication

Using this code to navigate through portfolio items.

<?php if($prev = $page->prevVisible()): ?>
    <a class="prev" href="<?php echo $prev->url() ?>"><span class="arrow">&larr;</span> previous</a>
<?php endif ?>

<?php if($next = $page->nextVisible()): ?>
    <a class="next" href="<?php echo $next->url() ?>">next <span class="arrow">&rarr;</span></a>
<?php endif ?>

However, it is also currently showing items that I don’t want visible unless the user has logged in. I am currently using this chunk of code below to sort out items that I don’t show on my main portfolio grid unless the user is a logged in user.

<?php if($site->user()) {

 $projects = $pages->find('portfolio')
                  ->children()
                  ->visible();                    

} else {

 $projects = $pages->find('portfolio')
                  ->children()
                  ->visible()
                  ->filterBy('nda', '!=', 'Yes');                 
}
?>

How should I amend the first chunk of code to hide those items while navigating between pages?

Thanks for your help.

You could use a page model like this:

<?php

class ProjectPage extends Page {
  public function prevVisible() {
    if(!$this->parent) {
      return null;
    } else {
      return $this->_prev($this->userAllowedSiblings(), func_get_args(), 'visible');
    }
  }
  
  public function nextVisible() {
    if(!$this->parent) {
      return null;
    } else {
      return $this->_next($this->userAllowedSiblings(), func_get_args(), 'visible');
    }
  }
  
  protected function userAllowedSiblings() {
    $pages = $this->parent->children();
    if(site()->user()) {
      $pages = $pages->filterBy('nda', '!=', 'Yes');
    }
    
    return $pages;
  }
}

Thank you for the suggestion. My development skills are a little bit green. I placed the code above in a file named gallery.php and put it in site>models directory. Now, a good portion of my other pages are not rendering.

If the template is called gallery, you need to name the model class GalleryPage.

That’s what I was thinking. The page renders now, but it’s not filtering out the hidden pages. I may have bitten off too much for myself. I really appreciate the help.

Oh, sorry. There’s a typo in my code above. It has to be:

  protected function userAllowedSiblings() {
    $pages = $this->parent->children();
    if(!site()->user()) {
      $pages = $pages->filterBy('nda', '!=', 'Yes');
    }
    
    return $pages;
  }

That did the trick! Thank you so much for the assistance.