yuters Posted June 9, 2016 Share Posted June 9, 2016 Hi, I'm not sure what I'm doing wrong here (Processwire 3.0.20), maybe it's just finding the right hook: public function init() { $this->pages->addHookAfter('saveReady', $this, 'adminFileAdded'); } public function adminFileAdded(HookEvent $event) { $page = $event->arguments(0); $filesAdded = $page->files->getItemsAdded(); } I've been getting an empty array when I add files to a page, it doesn't matter if I use save, saveReady, before and after. Here's some more info: 1. I'm seeing the field files has changed when I call $page->getChanges(); 2. $page->files->getItemsRemoved() shows the removed files correctly. 3. getItemsAdded() works correctly when I use a hook on Page::changed(files), but I still need to access the added items on the page save. this hook is called right after the upload, which I'm trying to avoid. Any help is appreciated, thanks Link to comment Share on other sites More sharing options...
Ivan Gretsky Posted June 9, 2016 Share Posted June 9, 2016 Welcome to the forums Mr. yuters! Just a guess... the files might not be actually added to WireArray untill the page is saved. So try $this->pages->addHookAfter('save', $this, 'adminFileAdded'); 2 Link to comment Share on other sites More sharing options...
horst Posted June 9, 2016 Share Posted June 9, 2016 (edited) Maybe a bit OT, but: If you add files to a page, you use a filefield (or imagefield). You haven't told what you want to do, but maybe you also can use other hooks? https://github.com/ryancramerdesign/ProcessWire/blob/devns/wire/modules/Inputfield/InputfieldFile/InputfieldFile.module#L457 https://github.com/ryancramerdesign/ProcessWire/blob/devns/wire/modules/Inputfield/InputfieldFile/InputfieldFile.module#L491 ... and welcome to the forums! Edited June 9, 2016 by horst Link to comment Share on other sites More sharing options...
yuters Posted June 10, 2016 Author Share Posted June 10, 2016 Thanks Ivan, yes I've tried that hook too with the same results, it doesn't matter if it's before or after "save" or "saveReady", the array is always empty. horst, what I'm trying to do is notify users about new files added to a specific page template. The problem with the hooks you provided is that they are called when I upload the file, not when I save the page. It has to be when the page is saved so we know all uploads are final. I just realized that even the top bar notifications when I save the page doesn't show what files have been added, but shows the files that have been deleted, so maybe there's actually no way to do this. For now I'm only notifying when the files field has been changed, but it would have been nice to have a list of new files easily. The only way I see around this is to keep an history of the files in the database and compare with the full new list (files->getItems()) Link to comment Share on other sites More sharing options...
horst Posted June 10, 2016 Share Posted June 10, 2016 Maybe you can keep it in the session only? When opening a page with the filefield, getItems() into a session var. After saving the page, compare it to the new getItems() values. You may even finegrain this a bit more: track changes of the filefield and only if there were changes, compare it with the new values. Link to comment Share on other sites More sharing options...
yuters Posted June 10, 2016 Author Share Posted June 10, 2016 That's a very good idea, thanks! 1 Link to comment Share on other sites More sharing options...
kongondo Posted June 10, 2016 Share Posted June 10, 2016 Hi Yuters...welcome to the forums.. I got a bit of joy by hooking into InputfieldFile::processInput similar to what was done here. Didn't test much further since my code kept interfering with the files ajax upload (i.e. would prevent uploads)... 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