OblikStudio / kirby-link-field

Links field for Kirby.
MIT License
77 stars 10 forks source link

Call to a member function url() on null - with toLinkObject() in Kirby 3.5.6 #48

Closed Maybach91 closed 2 years ago

Maybach91 commented 3 years ago

I’m trying to get the URL of the link field like so:

$link = $teammember->link()->toLinkObject();
var_dump($link->url());

I get the following error: Call to a member function url() on null

Blueprint:

link:
  label: Link (optional)
  help: z.B. zum Verlinken des Namens auf einen Lebenslauf
  type: link

$link has to following var_dump:

  ["data":protected]=>
  array(2) {
    ["type"]=>
    string(4) "page"
    ["value"]=>
    string(17) "vita-malte-flatau"
  }

And $teammember->link() the following var_dump:

  ["link"]=>
  array(2) {
    ["type"]=>
    string(4) "page"
    ["value"]=>
    string(17) "vita-malte-flatau"
  }

Whats wrong here? I just want the value. with $teammember->link()->value() i get the array as well.

"link": {
    "type": "page",
    "value": "vita-malte-flatau"
}
hdodov commented 2 years ago

Can you confirm that this still happens with the latest version of the plugin? I can't reproduce it.

Afplaktape commented 2 years ago

I'm having a similar issue where I try to check if the href exists to show or hide the button. I'm using the latest version of Kirby and also the latest version of the plugin. My code is:

<?php if ($page->button2()->toLinkObject()->href()->isNotEmpty()): ?> Button here <?php endif ?>

which gives the "Call to a member function href() on null" Also if I change it to popup() or title(). What can I do to get this to work?

hdodov commented 2 years ago

You need to check if the link object itself is not null:

<?php if ($page->button2()->toLinkObject() !== null): ?> Button here <?php endif ?>

However, that will soon change due to a change in the plugin related to issue #61. But until then, you can use the snippet I posted above.

hdodov commented 2 years ago

This is now changed in the new version of the plugin. Check the release notes.