fabianmichael / kirby-meta

All-in-one solution to all of your SEO/OpenGraph/JSON-LD needs. 👀
MIT License
65 stars 8 forks source link

Page model function 'metadata' not working #45

Closed CHE1RON closed 1 year ago

CHE1RON commented 1 year ago

Hey there,

this line will always return false even if such method exists, since custom page methods don't exist on the $page object. Problem is line 32:

// Get metadata from page, if possible
if (method_exists($this->page, 'metadata') === true) {  # << will always be false
    $this->metadata = $this->page->metadata($languageCode);
}

Because of this, $page->metadata is not populated, hence our check on line 86 never succeeds, rendering custom page method 'metadata' useless 😭 If a page method does not exist, Kirby assumes it's a Field with value null 😕

Solution:

hasMethod to the rescue:

// Get metadata from page, if possible
if ($this->page->hasMethod('metadata') === true) {  # << njom njom
    $this->metadata = $this->page->metadata($languageCode);
}

Voila! PR welcome, I guess? 🤣

CHE1RON commented 1 year ago

There it is!

offtopic: I'd like to see a new (patch) release after this getting merged, so I can further hack on your stuff in production 😆

fabianmichael commented 1 year ago

@CHE1RON Usually, metadata() is implemented on custom page models as a "real" method, so this was intentional. However, I added the possibility to use Kirby’s "virtual" methods as well, see 02f79b9.

CHE1RON commented 1 year ago

Thanks @fabianmichael for your feedback, I totally forgot about this - but my approach should work in both situations (= no need for both checks), or am I mistaken ❓

fabianmichael commented 1 year ago

@CHE1RON I’ve digged through Kirby’s source and it seems like the HasMethods trait only searches Kirby’s built-in methods.

CHE1RON commented 1 year ago

Alright, thanks for letting me know. If I can lend a hand, let me know, Discord or otherwise.