`Call to a member function url() on boolean`on selectfield for URL


#1

Hi,

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:

  hasbookingbtn:
    label: Zeige Buchenbutton mit Link auf Reservierungsseite
    text: Der Reservierungsbutton wird angezeigt wenn die Checkbox markiert ist.
    type: checkbox

  bookingbtntext:
    label: Buchenbuttontext
    type:  text

  bookingbtntarget:
    label: Buchenbutton Link Ziel
    type: select
    default: reservierung
    options: query
    query:
      fetch: children
      value: '{{uid}}'

  bookingbtncolor:
    label: Buchenbutton Hintergundfarbe
    type:  pickacolor

And the Snippet:

<?php

if($page->hasbookingbtn()->bool()): ?>
<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()) ?>
    </a>
</div>
<?php endif; ?>

#2

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.


#3

Hello Sonja.

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.


#4

Maybe you saved the UID instead of the Uri? And if the page is a subpage, it won’t work.


#5

Hi,
ja you are right. With this both I have a little bit problems to choose the right also in the past.