I’m attempting to create a new method $page->cat_color()
using a page model for my page.php
template.
On each page.php
-page, I am selecting a category
queried from a structure field on a categories
page.
I’m getting very confused by the following outcome:
// site/models/page.php
<?php
class PagePage extends Page {
public function cat_color() {
return page('categories')
->categories()
->toStructure()
->findBy('category', $this->category())
->color();
}
}
this breaks the page (“Call to a member function color() on null”).
However, if I replace $this->category()
with one of my predefined categories, i.e 'Curating'
, it’s working well:
return page('categories')
->categories()
->toStructure()
->findBy('category', 'Curating')
->color();
// returns the set hex value for 'Curating'
Inside my method ($this->category() == 'Curating');
is true for all the pages where i have selected the category, so whyy is the method not working using $this->category()
?
here’s the result of
var_dump(page('categories')->categories())
string(134)
"-
category: Curating
color: '#FFF77F'
-
category: Writing
color: '#B4FFDF'
-
category: Public Speaking
color: '#965AFF'
"
here’s the result of
var_dump($page('categories')->categories()->toStructure()
object(Kirby\Cms\Structure)#322 (3) {
[0]=>
int(0)
[1]=>
int(1)
[2]=>
int(2)
}