bytesource Posted May 19, 2013 Posted May 19, 2013 Hi, I wrote a helper function that takes an image field instance and returns its respective <img> ... /> HTML. After installing the Thumbnail module I wanted to always output the thumbnail if present. In order to determine of an image has an additional thumbnail, I tested for the 'getThumb' method that is added by the module to PageImage. However, the following code always returned the path to the original image, even if a thumbnail was present: $url = method_exists($image, 'getThumb') ? $image->getThumb('thumbnail') : $image->url; This leads me to the following questions: Why does the above code not work? Is there a better way to differentiate between a 'normal' image field and one that can contain a thumbnail? Cheers, Stefan
Wanze Posted May 19, 2013 Posted May 19, 2013 Hi bytesource, 1) This won't work because the getThumb method is added dynamically with a hook. Most likely 'method_exists' will look for static methods in classes, that's because false is returned. 2) After taking a short look at the description of the module, this could work: if ($imageField->type == 'FieldtypeCropImage') { //... Thumbnails }
ryan Posted May 19, 2013 Posted May 19, 2013 I think that you could do this: if(count($image->getHooks('getThumb'))) { $url = $image->getThumb('thumbnail'); } else { $url = $image->url(); } another way: try { $url = $image->getThumb('thumbnail'); } catch(Exception $e) { $url = $image->url(); } 1
bytesource Posted May 20, 2013 Author Posted May 20, 2013 @Wanze I tried your code, but $image->type does not produce any output. I have to admint I am not quite sure what FieldtypeCropImage stands for. As far as I understand $image is still an instance of PageImage, with a dynamically added 'getThumb' method. Indeed, the following always returns 'original image' if ($image instanceof PageImage) echo "original image"; else echo "thumbnail"; // => original image @ryan I like your second suggestion. It works perfectly. Cheers, Stefan
Wanze Posted May 20, 2013 Posted May 20, 2013 Sorry, I was wrong. type exists on the field object. So this should also work: if ($fields->get('yourImageFieldName')->type == 'FieldtypeCropImage') { // Thumbnail FieldtypeCropImage is the Fieldtype used by a thumbnails image field. 1
bytesource Posted May 21, 2013 Author Posted May 21, 2013 That works great, thanks a lot! Your solution finally made me realize that we are not only dealing with pages but also with different field types. Cheers, Stefan
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now