heldercervantes Posted December 6, 2021 Share Posted December 6, 2021 Hey guys. Another noob question about modules. I'm making a module that, when a page is saved, generates a json file that will be read from an outside app to get the latest news. I'm almost there, got the module working and generating the json file nicely, but I'm still struggling with images. Basically I want to output something like `$page->image->size(300,300)->url`, but that doesn't work saying size isn't available in this context. So I tried passing it without the size, but then the url only outputs the image's assets directory, not the file itself. Couldn't find anything in the docs ? Link to comment Share on other sites More sharing options...
bernhard Posted December 6, 2021 Share Posted December 6, 2021 I guess because you are in a module you have output formatting OFF which means that $page->image is an array of images and not a single PageImage object. Using $page->image->first()->size should work. PS: To make everything clear and reduce chances for errors you can be more verbose and use $page->getUnformatted('image')->first()... PPS: Better check if an image was uploaded before using ->size(...) $img = $page->getUnformatted('image'); if($img AND $img->count()) { $img = $img->first(); $img->size(...)->... } 1 2 Link to comment Share on other sites More sharing options...
heldercervantes Posted December 6, 2021 Author Share Posted December 6, 2021 5 minutes ago, bernhard said: I guess because you are in a module you have output formatting OFF which means that $page->image is an array of images and not a single PageImage object. Using $page->image->first()->size should work. That's it! Thanks. I owe you a beer ? 1 Link to comment Share on other sites More sharing options...
Jonathan Lahijani Posted June 7, 2023 Share Posted June 7, 2023 On 12/6/2021 at 5:03 AM, bernhard said: PS: To make everything clear and reduce chances for errors you can be more verbose and use $page->getUnformatted('image')->first()... I'm going to staple this to my head! Link to comment Share on other sites More sharing options...
bernhard Posted June 8, 2023 Share Posted June 8, 2023 14 hours ago, Jonathan Lahijani said: I'm going to staple this to my head! Please don't ? We have a new way of doing that, because ->getUnformatted() also gets temporary items, which is likely not what you want. This is how to do it in recent versions: $page->get("images.first") --> I've searched hard but did not find the blog post about it ? Anyone? 1 Link to comment Share on other sites More sharing options...
matjazp Posted June 8, 2023 Share Posted June 8, 2023 https://processwire.com/talk/topic/27528-weekly-update-– 2-september-2022/ 1 1 Link to comment Share on other sites More sharing options...
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