Overwrite page method to get field contents

Hi there,

I need help with the following problem.

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 in advance!


I suggest that you use a custom page method as described here: https://getkirby.com/docs/developer-guide/objects/page

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.

On which line do you get the array to string conversion error? I can’t spot the error.

However you could try using parent::categories()->split(',') in the last line, but I don’t think this is where the error comes from.


thanks for having a look into it. New day, new luck. It was indeed a simple, stupid mistake. :dizzy_face: It was just another method I hadn’t updated to work with the new type of output.

Thank you both! :slight_smile:

1 Like