Jump to content

files method getItemsAdded() always return an empty array


yuters
 Share

Recommended Posts

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

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?

... and welcome to the forums! :)

Edited by horst
Link to comment
Share on other sites

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

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

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

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