Hi all,
I have created a page model for an ‘article’ page. This model adds a function, which automatically generates a square preview version of the article’s main image. The simple code is as follows:
class ArticlePage extends Page {
public function square_img() {
$img = image($this->main_image());
$options = array(
'width' => 330,
'height' => 330,
'crop' => true
);
return thumb($img, $options);
}
}
In the article template, I can access $article->square_img()
without any problems, and it all works like a charm.
Now, the problem I’m finding is that at the end of every article, I have a list of “related articles”. That list is kept as a structured field - each entry containing just a ‘page’ field, titled ‘article_uri’. When I display this list at the end of my article, I’d like to show the “square_img” of all related articles. I’m trying to do this in a foreach()
loop, where I’m creating a page using the page()
helper, and then trying to fetch the square_img()
for that article page:
<?php
foreach ($page->related_articles()->toStructure() as $article):
$related = page($article->article_uri());
?>
<a href="<?php echo $related->url() ?>">
<h3><?php echo $related->title()->html() ?></h3>
<img src="<?php echo $related->square_img()->url() ?>">
</a>
<?php endforeach; ?>
It fetches $related->url()
and $related->title()
without any problems, but when it tries to fetch $related->square_img()
it generates a fatal error:
Fatal error: Uncaught The given image is invalid thrown in [...]/kirby/toolkit/lib/thumb.php on line 68
It seems to me, that when I fetch each article in my foreach
loop using the page()
helper, Kirby is NOT loading the required page model. This doesn’t seem right, as according to the docs, it should be.
The workaround is to put all the thumb code inside the template, creating the appropriate image objects from manually-composed uris inside the loop. This, however, would really be better placed inside the model for the article - keeping the template simpler, which is the whole purpose of the model…
Am I missing something, or is this a bug?..