I have a selectfield for choosing a childrenpage as linktarget I call this with
if($page->bookingbtntarget()->isNotEmpty()): echo $page->bookingbtntarget()->toPage()->url(); endif;
but I get the error
Call to a member function url() on boolean
I’m a little bit confused because I proofed this and the
->toPage()->url()is the right way to get the whole URL for the link.
If I delete the
toPage()it work but I got a wrong build url.
This is the blueprint:
label: Zeige Buchenbutton mit Link auf Reservierungsseite
text: Der Reservierungsbutton wird angezeigt wenn die Checkbox markiert ist.
label: Buchenbutton Link Ziel
label: Buchenbutton Hintergundfarbe
And the Snippet:
<div class="bookingcom col-xs-24 col-sm-24 col-md-24 col-ld-24">
<a class="btn btn-red bookingbtn col-xs-20 col-sm-20 col-md-24 col-lg-24 ajaxlink" href="<?php if($page->bookingbtntarget()->isNotEmpty()): echo $page->bookingbtntarget()->toPage()->url(); endif; ?>" data-name="booking" <?php if(!$page->bookingbtncolor()->empty()): ?>style="background: <?php echo $page->bookingbtncolor()->value(); endif; ?>">
<?php echo html($page->bookingbtntext()) ?>
<?php endif; ?>
Although I keep repeating it: Never call an object method without prior checking if the object exists. It’s not important if your field contains anything, it is important that the object - in this case the page - exists. This is necessary because at the time of saving, your page might exists. But what if it is deleted later, or renamed? Then you have a value in your field - but the page is gone and your
toPage()method can’t create a page object. Then you end up with a broken page.
`Nough theory, let’s do it:
<?= ($p = $page->bookingbtntarget()->toPage())? $p->url():null; ?>
With this code, you don’t have to check if the field contains a value or not. If it is empty, you won’t have a page.
By the way: To avoid the problem with renamed pages, the AutoID plugin is a good choice.
thank you for your explanation what I’m doing wrong!
This ternary operator is now ending with null. But the page exist and I can call it directly.
To solve it for me now, because I have too much time invested to solve this problem I wrote it as
if($page->bookingbtntarget()->isNotEmpty()): echo $page->url() .'/'.$page->bookingbtntarget()->uid(); endif;
I know this is really crap. But I need to get finish.
For me personally I’ll investigation further why the object is here not present.
Maybe you saved the UID instead of the Uri? And if the page is a subpage, it won’t work.
ja you are right. With this both I have a little bit problems to choose the right also in the past.