Jump to content

PageImage multiple, getting first or random and applying size returns "Fatal Error: Call to a member function size() on null"


PascalKonings
 Share

Recommended Posts

Hi all,

I have an images field on my template, which can contain multiple images, returning an array.
When trying to get either first() or getRandom() image of this array and then applying size(width, height) to it, the image will render properly resized, however in the PW logs I find:

Fatal Error: Call to a member function size() on null.

I can't understand why. When limiting the field to only 1 image and applying size, no error occurs. Also when looping through my array and applying size to each individual image, no error occurs.

Does anyone have any clue as to why the image renders properly, but the log gets filled up with an error?

 

// No errors
foreach($page->banner as $image) {
$thumb = $image->size(200, 200);
echo "<a href='$image->url'>";
echo "<img src='$thumb->url' alt='$image->description' />";
echo "</a>";
}
 
// Error in the logs, however rendering properly
$random
= $page->banner->getRandom();
$thumb = $random->size(200, 200);
echo "<a href='$random->url'>";
echo "<img src='$thumb->url' alt='$random->description' />";
echo "</a>";
Link to comment
Share on other sites

Hey, I would do a type check on the return of banner->getRandom(). You do not check if there is always at least one image available.

Or just temporary try this for a while and see if it behaves the same with the log filling:

$random = $page->getUnformatted("banner")->findRandom(1);
if(0 < count($random)) {
    $thumb = $random->first()->size(200, 200);

	echo "<a href='$thumb->url'>";
	echo "<img src='$thumb->url' alt='$thumb->description' />";
	echo "</a>";
}

 

  • Like 3
Link to comment
Share on other sites

Thanks @horst,

Adding a count check did in fact solve the problem.

if(count($page->banner) > 0) {
$thumb = $page->banner->getRandom()->size(200, 200);
echo "<a href='$thumb->url'>";
echo "<img src='$thumb->url' alt='$thumb->description' />";
echo "</a>";
}
  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...