markus_blue_tomato Posted October 24, 2018 Share Posted October 24, 2018 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 More sharing options...
Robin S Posted October 24, 2018 Share Posted October 24, 2018 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(). 2 Link to comment Share on other sites More sharing options...
markus_blue_tomato Posted October 25, 2018 Author Share Posted October 25, 2018 Yes this was only an issue of my example code. In my real module I use this: $this->addHookAfter('Pagefile::url', $this, 'addLoremIpsumQueryParameters'); I tried it with the ->first() method but only got NULL ? Link to comment Share on other sites More sharing options...
markus_blue_tomato Posted October 25, 2018 Author Share Posted October 25, 2018 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 More sharing options...
markus_blue_tomato Posted October 25, 2018 Author Share Posted October 25, 2018 aaaah. holy shit sorry. my fault! I have 2 image fields in my template and the field which I tested was empty. But I was worried why I got an asset-path back and not NULL or else... Now with an uploaded image in the field it works. 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