I have several product pages. The categories field contains a comma-separated list of one or categories. These categories are actually just references to a corresponding category page.
In order to simplify the code in my tempaltes I’d like $page->categories()to return a collection or an array of category page objects instead of the comma-separated list of categories.
I’m trying to solve this with a custom page method.
public function categories() {
return array_map(function($item) {
return page('produkte')->children()->find($item);
}, $this->content()->get('categories')->split(','));
}
However, I’m getting an array-to-string conversion and am not sure wether this is just a stupid mistake or a side effect of redeclaring the categories getter-method. Does anyone have a deeper knowledge of how Kirby works or is able to tell me what would be the best practice in this case?
Thanks for taking the time to answer! I knew of the possibility to add default page methods (and am using this in some other places). However, this is something specific to product pages and I thought it would be a good idea to use a page model in this case.
Actually I want the categoriesmethod to behave differently when called on different page types. For example calling the method on the home page returns all available categories while calling it on a product page would return - as described – the product’s categories.
thanks for having a look into it. New day, new luck. It was indeed a simple, stupid mistake. It was just another method I hadn’t updated to work with the new type of output.