Jump to content

addHookAfter on Pagefile::url are not working within addHookAfter Page::save


markus_blue_tomato
 Share

Recommended Posts

I have an hook in an module, where I do some URL manipulation on my Imagefiles. This works really nice in the frontend. (e.g. add query parameters)

/*
... some module code...
*/
public function init() {
   $this->addHookAfter('Pagefile::url', $this, function(HookEvent $event) {
    if(!$event->object instanceof Pagefile) { return; }
      $file = $event->object;
      $fileBasename = $file->basename;
      $event->return = $fileBasename . '?lorem=ipsum';
   });
}

Now I'm developing an module where I send my pages to ElasticSearch after saving the page with an Hook after the save.

public function init() {
	$this->pages->addHookAfter('save', $this, function(HookEvent $event) {
		$page = $event->arguments[0];
		
		// hook does not form. query parameters are not added
		$image = $page->my_image_field->url; 

	});
}

But within the Page::save Hook, the Pagefile::url Hook is not executed.  The whole module init from the Pagefile::url is executed but the Hook itself not.

Does anyone know what I'm doing wrong? Or is this not possible?

 

Link to comment
Share on other sites

It's working for me here...

<?php namespace ProcessWire;

class TestModule extends WireData implements Module {

    public static function getModuleInfo() {
        return array(
            'title' => "Test Module",
            'version' => 1,
            'autoload' => true,
        );
    }

    public function init() {

        $this->addHookAfter('Pagefile::url', function(HookEvent $event) {
            if(!$event->object instanceof Pagefile) { return; }
            $file = $event->object;
            $fileBasename = $file->basename;
            $event->return = $fileBasename . '?lorem=ipsum';
        });

        $this->pages->addHookAfter('save', function(HookEvent $event) {
            $page = $event->arguments(0);
            if($page->hasField('image')) {
                // Output formatting will probably be off, but get unformatted value to be sure
                $pageimages = $page->getUnformatted('image');
                // Get first image URL
                $url = $pageimages->first()->url;
                // Dump URL with Tracy Debugger
                bd($url, 'url');
            }
        });

    }

}

You shouldn't use $this as the second argument to addHookAfter() if your hook function is a closure, but I'm guessing that is just an error in the demo code you posted and not in your actual module.

Your issue might be due to output formatting being off in the Pages::save() hook which means an Images field will return a Pageimages object which doesn't have a URL property. So if the field holds only a single Pageimage you would get that with first().  

  • Like 2
Link to comment
Share on other sites

And this is my var_dump vom the image field:

object(ProcessWire\Pageimages)#1233 (6) { ["count"]=> int(0) ["page"]=> string(66) "/en/blue-world/snowboard/nash-finale-2018-next-austrian-snow-hero/" ["field"]=> string(17) "news_previewimage" ["url"]=> string(24) "/site/assets/files/2746/" ["path"]=> string(37) "/var/www/html/site/assets/files/2746/" ["items"]=> array(0) { } }

 

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