bencarr / statamic-hyperlink

Hyperlink is a Statamic addon that expands the default Link fieldtype and allows you to store link text and target options alongside the link destination.
https://statamic.com/addons/bencarr/hyperlink
3 stars 1 forks source link

Deleting a linked entry causes page to throw `Call to a member function url() on null` #13

Closed aaronbushnell closed 3 months ago

aaronbushnell commented 3 months ago

I noticed that if I:

  1. Create a page called "Test"
  2. Add a hyperlink on the "Home" entry to it
  3. Delete "Test"

That my "Home" entry throws an error on the front-end and back-end. It reads:

vendor/bencarr/statamic-hyperlink/src/Helpers/HyperlinkData.php:118

Call to a member function url() on null

I presume it's because the data still is trying to read a URL from an entry that no longer exists. Could a condition wrap this so that this error doesn't occur and so that the Hyperlink field removes this data on the next save of the source entry?

bencarr commented 3 months ago

Thanks for the concise steps, Aaron! v2.0.1 is out now with a fix for this issue. Link fields now fail more gracefully (as in, without a PHP error) when the related element can't be found. Under the hood, the url property will now be null in those instances.

Visiting the entry in the control panel will highlight the invalid reference just like Statamic would on a regular entries/assets/terms field, and let authors clean up the relationship.

image
aaronbushnell commented 3 months ago

No problem, thanks so much @bencarr!