FireWire Posted February 12 Share Posted February 12 Having an issue where configuring the "Formatted value" for image fields is not returning the value as configured when accessing the field within a hook. It is configured to return Pageimage/null. If I access the field within a template then the return value is correct, Pageimage/null I have the following page class: <?php class EventActivityPage { /** * Gets hero image or fallback hero image if one does not exist for this activity */ public function heroImage(): Pageimage { return $this->hero_image ?? $this->eventPage()->activity_fallback_hero_image; } } The heroImage() method returns the correct Pageimage/null value when accessed from a template, or from another method in that page class if called from a template. I have the following hook: <?php wire()->addHookAfter('Pages::saveReady(template=event_activity)', function(HookEvent $e) { $page = $e->arguments('page'); $page->heroImage(); // Returns Pageimages rather than a Pageimage $page->hero_image; // Accessing the field directly within a hook returns Pageimages rather than a Pageimage }); Not sure what's going on and I'm hesitant to dive into the internals if there may be an easier explanation. ProcessWire v3.0.244 PHP v8.3.14 Thanks! Link to comment Share on other sites More sharing options...
FireWire Posted February 12 Author Share Posted February 12 Accessing an image field within a hook also appears to break the fallback image. Accessing the image field within a hook returns a Pageimages object with a count of 0. Link to comment Share on other sites More sharing options...
monollonom Posted February 12 Share Posted February 12 Is it possible the output formatting is off within the saveReady hook? A PageImage will always default to a PageImages regardless of this option when the output formatting is off. 2 Link to comment Share on other sites More sharing options...
FireWire Posted February 26 Author Share Posted February 26 @monollonom Thanks for the insight. I haven't had a chance to work on that yet but I'll report back. I didn't think to check that output formatting would be the culprit since it's changing the output class itself whereas output formatting on other fields changes the value output to the page, like a text field for example. I'll report back when I have a chance to tinker. Link to comment Share on other sites More sharing options...
bernhard Posted February 27 Share Posted February 27 <?php class EventActivityPage { /** * Gets hero image or fallback hero image if one does not exist for this activity */ public function heroImage(): Pageimage { return $this->getFormatted('hero_image.first') ?: $this->eventPage()->getFormatted('activity_fallback_hero_image.first'); } } Using getFormatted + .first you can force the return type so no matter what setting or output formatting is applied your method will return the correct value. 1 Link to comment Share on other sites More sharing options...
FireWire Posted February 27 Author Share Posted February 27 @bernhard Outstanding! Yet another great PW feature I wasn't aware of. Thanks for sharing! Link to comment Share on other sites More sharing options...
bernhard Posted February 27 Share Posted February 27 Yeah! Really useful feature. Has been a quite long way to get it 🙂 See https://processwire.com/talk/topic/27528-weekly-update-– 2-september-2022/ for all available options. Even the link was quite hard to find, so no wonder not many know about it... 2 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