Filter next() and prev()


#1

How can I use the next() and prev() or it´s brothers/sisters with the filterBy() method?

I want to check if the date and if it is in the future I don´t want the next() link to appear. Ideas?


#2

My code…

My articles code from the site/templates/articles.php and it works perfectly.

<?php
          date_default_timezone_set('America/Sao_Paulo');
          $articles = $pages->find('articles')
          ->children()
          ->visible()
          ->filterBy('date', '<=', strtotime(date_default_timezone_get(date('Y-m-d'))))
          ->flip()
          ->paginate(4);
          ?>

I wish to add the equal to the filterBy() check here to the next and prev buttons. Is is possible?


#3

Have a look at the custom methods here:


#4

Yes. I tried this code. But it failed for me yesterday. Not sure if I added it correctly. How will you add this @texnixe? :blush:


#5

You pass the filtered collection as an argument… optionally a sort parameter.

$page->getNext($collection)

#6

Do I understand you correctly if this how I need to go around this than?

$page->getNext(filterBy('date', '<=', strtotime(date_default_timezone_get(date('Y-m-d')))))

Will this work? But is this the way I call this in the template, right?

But the code over where do I apply it, or add it? What is the correct path?


#7

Not quite, should be like this:

 $articles = $pages->find('articles')
          ->children()
          ->visible()
          ->filterBy('date', '<=', strtotime(date_default_timezone_get(date('Y-m-d'))))
          ->flip();

foreach($articles as $article):
  $next = $article->getNext($articles);
  $prev = $article->getPrev($articles);
endforeach;

As I said above, the method wants the filtered collection (in this case $articles) as a parameter.


#8

Where do I place the custom methods?


#9

In a plugin file, e.g a site/plugins/methods.php


#10

Not sure if I get this strait yet. Sorry for being slow here. Trying really hard to understand this. :sweat:

So on my article.php page I will need to do this how, where I want my next and prev links?

With normal ways should be something like this:

<?php if($page->hasNextVisible()): ?>
  <div class="pull-right">
    <a href="<?php echo $page->nextVisible()->url() ?>"><?php echo $page->nextVisible()->title()->excerpt(20) ?></a>
  </div>
<?php endif ?>

<?php if($page->hasPrevVisible()): ?>
  <div class="pull-left">
    <a href="<?php echo $page->prevVisible()->url() ?>"><?php echo $page->prevVisible()->title()->excerpt(20) ?></a>
  </div>
<?php endif ?>

I am just unsure how to apply this code to get the wanted result. :rocket:


#11

You have to define your $articles collection in the article.php controller or template as well, otherwise it will be undefined.

We can’t use the hasNextVisible() and hasPrevVisible() methods here or we would have create custom methods, but we don’t necessarily need them.

<?php if($next = $page->getNext($articles)): ?>
  <div class="pull-right">
    <a href="<?= $next->url() ?>"><?= $next->title()->excerpt(20) ?></a>
  </div>
<?php endif ?>

<?php if($prev = $page->getPrev($articles)): ?>
  <div class="pull-left">
    <a href="<?= $prev->url() ?>"><?= $prev->title()->excerpt(20) ?></a>
  </div>
<?php endif ?>

Since our collection already contains the visibility filter, we don’t have to apply it again.


#12

Now I follow you. Thank you @texnixe. :smile::smile::smile:


#13

Well, glad that it works now. :sweat_smile:


#14

I still need to add it to the project. I now have an idea how to do this. :slight_smile:


#15

Well, good luck! I’m here if you need me…


#16

This worked out perfectly. Thank you for the help @texnixe. :smile: