Why Page has no prevVisible


#1

Hi there,

I have a Page where has children Pages and all of them are visible. So I wanted to include a Prev/Next Navigation where I have done some other times. But this time, also when I dump() the Page Object, there is no PrevVisible or nextVisible. But I’m 100% sure that they are available. Can this have any other reasons?


#2

It’s hard to say without seeing the code you are trying, but something like this should work, and its pretty much whats in the Kirby documentation. I know the below works, since I just grabbed it from my own site.

<?php

$directionPrev = @$flip ? 'right' : 'left';
$directionNext = @$flip ? 'left'  : 'right';

if($page->hasNextVisible() || $page->hasPrevVisible()): ?>
  <nav class="nextprev <?= !@$flip ?: ' flip' ?>">

    <?php if($page->hasPrevVisible()): ?>
      <a class="button <?= $directionPrev ?>" href="<?= $page->prevVisible()->url() ?>" rel="prev" title="<?= $page->prevVisible()->title()->html() ?>">
        &larr; Previous
      </a>
    <?php endif ?>

    <?php if($page->hasNextVisible()): ?>
      <a class="button <?= $directionNext ?>" href="<?= $page->nextVisible()->url() ?>" rel="next" title="<?= $page->nextVisible()->title()->html() ?>">
        Next &rarr;
      </a>
    <?php endif ?>
  </nav>
<?php endif ?>

#3

Instead of this, I`d check for visible siblings…

@Oli1 Where have you included the navigation? I’m a bit irritated by your dumping of the page object… because it tells you if a page has siblings but not if these are visible or not…


#4

Well errmmm… I went with Bastians version in the Starterkit. What is the advantage of checking for siblings instead?


#5

There’s absolutely nothing wrong with your code, but somehow this || statement produces unnecessary long code and duplicate checks. If a page has visible siblings, one of the above conditions is automatically true. Also, I’m not a fan of using @ in front of a variable to suppress errors if the variable is not defined…


#6

Hi there,

that’s true, if I dump() the Page, I get some siblings, but then for what is the hasPrevVisible() function? And normally, if I dump() the Page, it should output a boolean? Here’s the output of the dump().

What’s really strange, I checked the Code in another site and there it’s working?? When does the Page Object tells me if there are prev or next? I included the code directly in the template file. The parent template is “faqs” and the childs does have the template “faq”.

Page Object
(
    [title] => Wo sind die rechtlichen Grundlagen für den Explosionsschutz zu finden?
    [id] => faqs/wo-sind-die-rechtlichen-grundlagen-fuer-den-explosionsschutz-zu-finden
    [uid] => wo-sind-die-rechtlichen-grundlagen-fuer-den-explosionsschutz-zu-finden
    [slug] => wo-sind-die-rechtlichen-grundlagen-fuer-den-explosionsschutz-zu-finden
    [parent] => faqs
    [uri] => /faqs/wo-sind-die-rechtlichen-grundlagen-fuer-den-explosionsschutz-zu-finden
    [url] => /faqs/wo-sind-die-rechtlichen-grundlagen-fuer-den-explosionsschutz-zu-finden
    [contentUrl] => /content/5-faqs/1-wo-sind-die-rechtlichen-grundlagen-fuer-den-explosionsschutz-zu-finden
    [tinyUrl] => /x/3lh5bb
    [root] => /xxx/content/5-faqs/1-wo-sind-die-rechtlichen-grundlagen-fuer-den-explosionsschutz-zu-finden
    [dirname] => 1-wo-sind-die-rechtlichen-grundlagen-fuer-den-explosionsschutz-zu-finden
    [diruri] => 5-faqs/1-wo-sind-die-rechtlichen-grundlagen-fuer-den-explosionsschutz-zu-finden
    [depth] => 2
    [num] => 1
    [hash] => 3lh5bb
    [modified] => 2018-10-10T20:30:15+00:00
    [template] => faq
    [intendedTemplate] => faq
    [isVisible] => 1
    [isOpen] => 1
    [isActive] => 1
    [isHomePage] => 
    [isErrorPage] => 
    [isCachable] => 1
    [isWritable] => 1
    [content] => Content Object
        (
            [root] => /xxx/content/5-faqs/1-wo-sind-die-rechtlichen-grundlagen-fuer-den-explosionsschutz-zu-finden/faq.txt
            [fields] => Array
                (
                    [tab1] => 
                    [title] => Wo sind die rechtlichen Grundlagen für den Explosionsschutz zu finden?
                    [accordion] => 
                    [tab2] => 
                    [headline] => 
                    [cta] => 
                    [tab3] => 
                    [metatitle] => 
                    [metadescription] => 
                    [text] => Für Hersteller gilt seit 20.4.2016 die EU Richtlinie 2014/34/EU (ATEX 114). Für Benutzer ist dies seit 1.6.2015 in der Gefahrstoffverordnung verankert, die die EG Richtlinie 99/92/EG umsetzt. Die erforderlichen Prüfungen sind in der Betriebssicherheitsverordnung geregelt.
                    [categories] => filter-explosionsschutz, filter-whg-fachbetrieb
                    [aufrufe] => 73
                )

        )

    [headers] => 
    [children] => Children Object
        (
        )

    [siblings] => Children Object
        (
            [0] => faqs/wer-ist-fuer-den-sicheren-betrieb-einer-explosionsgefaehrdeten-anlage-verantwortlich
            [1] => faqs/ab-wann-spricht-man-von-einer-ueberwachungsbeduerftigen-explosionsgefaehrdeten-anlage
            [2] => faqs/was-muss-der-auftraggeber-tun-wenn-er-eine-ueberwachungsbeduerftige-explosionsgefaehrdete-anlage-hat
            [3] => faqs/was-muss-geprueft-werden-und-wie-sind-die-pruefintervalle
            [4] => faqs/wie-hoch-sind-die-kosten-fuer-eine-pruefung
        )

    [files] => Files Object
        (
        )

)

#7

No, if you dump a page object, you get a page object, what you’ve got there is perfect. Could you post your prev/next navigation code? Or is it the same as what @jimbobrjames posted?


#8

The page object doesn’t tell you if there are any visible prev or next pages; it tells you that there are siblings. To check if the page has any visible siblings, you can do the following

dump($page->siblings(false)->visible();

The false parameter tells the function not to include the current page. You can remove the parameter, then you will actually see the current page at it’s position within the collection.

hasPrevVisible()/hasNextVisible()check if the current page has a visible sibling in a position prior to/after itself in the collection, they return a boolean.


#9

Hi Sonja,

here’s the Prev/Next Code, it’s diretly in the Template.

<?php if ( $page->hasPrevVisible() ) : ?>
  
<a class="btn btn-secondary mr10" href="<?php echo $page->prevVisible()->url(); ?>" title="<?php echo $page->prevVisible()->title(); ?>">
  <i class="ion-android-arrow-back"></i>
</a>

<?php endif; ?>

<?php if ( $page->hasNextVisible() ) : ?>
  
<a class="btn btn-secondary ml10" href="<?php echo $page->nextVisible()->url(); ?>" title="<?php echo $page->nextVisible()->title(); ?>">
  <i class="ion-android-arrow-forward"></i>
</a>

<?php endif; ?>

The dump($page->siblings(false)->visible(); Function returns this:

Children Object
(
    [0] => faqs/wer-ist-fuer-den-sicheren-betrieb-einer-explosionsgefaehrdeten-anlage-verantwortlich
    [1] => faqs/ab-wann-spricht-man-von-einer-ueberwachungsbeduerftigen-explosionsgefaehrdeten-anlage
    [2] => faqs/was-muss-der-auftraggeber-tun-wenn-er-eine-ueberwachungsbeduerftige-explosionsgefaehrdete-anlage-hat
    [3] => faqs/was-muss-geprueft-werden-und-wie-sind-die-pruefintervalle
    [4] => faqs/wie-hoch-sind-die-kosten-fuer-eine-pruefung
)

#10

Hm, that looks ok, and you really don‘t get anything printed, not even in the source code?


#11

Absolute nothing :-1: also not when I “echo” a simple string. Can this be a conflict with a widget or similar?


#12

What plugins do you have installed? If you don’t get it to work, feel free to send me your project. I‘m on the road today, but could take a look tomorrow.


#13

Hi @texnixe ,

I have found the Bug and it’s really strange (for me) … problem was in my Snippet header.php. I made there a Counter Increment like this:

$page->increment('hitcounter');

And this breaks the prev/next function. Any idea why? I put it now in the footer.php and this is working! The prev/next function is alive again :smiley:

PS: have edited the post and marked you. Not sure if this is a bug in Kirby?


#14

Not quite sure what’s actually happening there, maybe it is indeed a bug. But if you explicitly pass the page to the snippet like this, it works:

<?php $page->increment('field') ?>
<?php snippet('prevnext', ['page' => page($page->uri()),'flip' => true]) ?>

It would maybe make more sense to increment the page counter via an Ajax call on page load.


#15

Thanks for watching :slight_smile: maybe I made a error somewhere else where breaks this function.


#16

No, no, you didn’t. I tested this and had the same problem as soon as I added the page increment function prior to calling the prev-next snippet.


#17

Ahh ok. So seems if we have found a small bug :bug: