Jump to content

Error (getThumb) on image field


Peter Knight
 Share

Recommended Posts

Can someone sanity check this for me?

I have a field called "Featured_Image" and a crop setting called "thumbnail".

To outout this thumbnail image, I am trying the following code:

<img src={$featured->Featured_Image->first()->getThumb('thumbnail')} />

Currently it's throwing an error and I'm not sure if it means either

  1. My module isn't installed properly
  2. I'm calling the image incorrectly
  3. etc

Error: Call to a member function getThumb() on a non-object (line 9 of /Users/mymac/Sites/SiteDev/site/templates/includes/get-featured.inc) 

Link to comment
Share on other sites

if $featured->Featured_Image->first() isn't an object that has a method getThumb, so what is it actually?

or lets have a look at one step before: what does $featured->Featured_Image hold? (an images array, a single image, nothing?)

Link to comment
Share on other sites

but if it has a single image in it, why do you try to select the ->first() ?  I assume you have set the image field to allow only 1 image?

Also, if you have set it proper to a single image field, it could be that you have a page without an uploaded image to that field.

if($featured->Featured_Image) {
    echo "<img src=\"{$featured->Featured_Image->getThumb('thumbnail')}\" />";
}

------

Also my questions a post above was thought a bit different. I was trying to get you debug the output in your page, just above the line that raises the error. But I wasn't clear, so it doesn't :).

The error messages said that it isn't an object or the wrong object, so you can debug it to see what's going on. You expect it to be a cropimage field, therefore it should be an instance of type "FieldtypeCropImage".

if($featured->Featured_Image->first() instanceof FieldtypeCropImage) { echo "\n<p>the Featured_Image->first() is a cropimage</p>\n"; }
if($featured->Featured_Image instanceof FieldtypeCropImage) { echo "\n<p>the Featured_Image is a cropimage</p>\n"; }
// imagefield is: FieldtypeImage

Most collections in PW are based on WireArrays, so multiple images fields are too and you can look for that:

// or checking for single vs multiple
if($featured->Featured_Image instanceof WireArray) { 
    echo "\n<p>it is multiple</p>\n";
} else {
    echo "\n<p>it is single</p>\n";
}

You can also just dump a variable with var_dump($featured->Featured_Image->first()) to see what is in it, but that's mostly not very usefull in PW because objects in PW have way to many references to other objects so that var_dump results into very, very large outputs.

  • Like 5
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...