robzyye
September 7, 2018, 4:44pm
1
Hi,
I would like to know if it’s possible to make Prev/Next page link with some filterby… for exemple : Next Page Visible, Unscheduled (plugin), template Article etc…
<?php if($previous = $page->prev()->visible()->unscheduled()->filterBy('template', '==', 'blogarticle')): ?>
<a href="<?= $previous->url() ?>">previous articles</a>
<?php endif ?>
Thanks
texnixe
September 7, 2018, 4:50pm
2
It’s possible with these two custom methods:
<?php
// get next page of a given children collection (e.g. filtered collection)
page::$methods['getNext'] = function($page, Children $siblings, $sort = array(), $visibility = false) {
if($sort) $siblings = call(array($siblings, 'sortBy'), $sort);
$index = $siblings->indexOf($page);
if($index === false) return null;
if($visibility) {
$siblings = $siblings->offset($index+1);
$siblings = $siblings->{$visibility}();
return $siblings->first();
} else {
return $siblings->nth($index + 1);
}
};
<?php
// get prev page of a given children collection (e.g. filtered collection)
page::$methods['getPrev'] = function($page, Children $siblings, $sort = array(), $visibility = false) {
if($sort) $siblings = call(array($siblings, 'sortBy'), $sort);
$index = $siblings->indexOf($page);
if($index === false or $index === 0) return null;
if($visibility) {
$siblings = $siblings->limit($index);
$siblings = $siblings->{$visibility}();
return $siblings->last();
} else {
return $siblings->nth($index - 1);
}
};
They allow you to pass the filtered collection as first argument.
<?php
$siblings = $page->siblings()->visible()->filterBy('template', '==', 'blogarticle')->unscheduled();
if($previous = $page->getPrev($siblings)): ?>
<a href="<?= $previous->url() ?>">previous articles</a>
<?php endif ?>
robzyye
September 7, 2018, 5:23pm
3
Thank you a lot @texnixe when I see the code you have made, I could not have done it alone !