Jump to content

Image field not returning the correct value when accessed within a saveReady hook


Recommended Posts

Posted

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.

image.png.9820412ce64f2216fb1f0da3bcfabcb5.png

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!

Posted

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.

image.png.320684b69f9a3dec3c42b36b5b677388.png

Posted

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.

  • Like 2
  • 2 weeks later...
Posted

@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.

Posted
<?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.

 

  • Thanks 1

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
  • Recently Browsing   0 members

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