Hi folks,
maybe you can help me in finding out what I am overlooking. I am trying to put together the previous sibling and next sibling if they exist. However, I am failing…
if($page->hasSiblings()) {
$items[] = [
'url' => $prev = $page->prev() ? $prev->url() : null,
'label' => '<i class="fa fa-arrow-left"></i>',
'class' => 'sibling' . ($prev ?: ' sibling--empty')
];
$items[] = [
'url' => $next = $page->next() ? $next->url() : null,
'label' => '<i class="fa fa-arrow-right"></i>',
'class' => 'sibling' . ($next ?: ' sibling--empty')
];
}
Even though the script enters the if-condition (so it hasSiblings… and yes it does), prev()
as well as next()
returning NULL
. Shouldn’t at least one return a sibling, if hasSiblings()
is true?
Yes, it should and a quick test revealed that the code works as expected as well (apart from the class assignment):
'class' => 'sibling' . ($prev ? '': ' sibling--empty')
($prev ?: ' sibling--empty')
works as shorthand
In my fresh starterkit the code does not work as expected… but then I just realized, there isn’t a hasSiblings()
That’s what I guessed, but this is the result I get from the shorthand:
Array
(
[0] => Array
(
[url] => http://localhost/kirby241
[label] =>
[class] => siblinghttp://localhost/kirby241
)
[1] => Array
(
[url] =>
[label] =>
[class] => sibling sibling--empty
)
)
Ah right, good call! Thanks
Haha, right ! Then
if($page->siblings(false)->count()) {
}
or create a custom hasSiblings method.
Created a custom page method
And I found the real problem, why next()
would only return NULL
- I loaded $page
with the page class of the panel – somehow those methods don’t work well with the panel $page
. Forced it to be based on the normal Kirby page class and it works