Return something from the hooks

I just posted a plugin a made here: Tinypng compressor plugin

It’s amazing how simple things are in Kirby.

Success or failure?

In my plugin I compress the uploaded image with a hook in the panel. However I don’t know if the task was successful or not because there are no return value from the hook.

I want to know if it failed.

Return array?

Maybe it would be nice to return an array, like this?

$array = array(
    'success' => true,
    'message' => 'You compressed filename23.png successfully!'
);

Then show it as a message somewhere?

Om multiple hooks functions

One hook can be used by more than one function. It means multiple messages in the process. Maybe show / hide all the messages and show just the total success (if all went well) or the failure (if something went wrong), like this…

2 tasks failed. Show more info.
1 Like

Yes, I agree. I gave it a brief shot and the plugin works great so far, only thing is that it takes a while so it would also be great if there was a message that something is going on otherwise the user might be at a loss why it takes so long to upload the image ?

@texnixe I agree, that would be great as well.

I’m also looking for a way to give the user some kind of visual feedback while I generate a lot of different image sizes for each uploaded image. I use the new upload hook like this:

kirby()->hook('panel.file.upload', function($file) {

  if ($file->type() == 'image') {
    thumb($file, array('width' => 1500));
    thumb($file, array('width' => 1000));
    thumb($file, array('width' => 750));
    thumb($file, array('width' => 500));
    thumb($file, array('width' => 450));
    thumb($file, array('width' => 320));
  }
  
});

If the user upload, let say, 30 images the server have to do a lot of image processing and the user then have no idea off whats going on for a long time. Probably think some thing went wrong and might try to upload again or hit the back button. Would be nice to have a progress bar like the one showing during the upload. But just a message would be great too.

1 Like

As you say the image uploader really needs some improvement.

3 of 40 images successfully uploaded

Then there are more things to think about.

  • What if one image failes? Should it abort or continue?
  • What if some hook on one image failes, should it abort or continue?
  • How should it communicate that 3 images was uploaded successfully but 6 tasks in 2 hooks failed?
  • What tasked failed in what hooks? Can I retry just the failed ones?

I see it can become quite complex but to make it great these questions might need answers?

@bastianallgeier Is this some ting that will come in the future? A better image upload function to make it possible to generate different image sizes on upload is my no 1 feature request.

Return something from the hooks would also be great for custom ones in plugins.

That would make it possible for many plugins to use the same component for example (place a hook inside a component).

It would also be great to filter stuff. Take args and output something else.

Would it be hard to implement a return feature?

A progress message is currently not possible, but you should already be able to communicate the result of your hook using:

panel()->notify('Yay, it worked.');
panel()->alert('Something went wrong.');

The notification will appear in the top right and will replace the :) smiley for successful save operations.

Please note that there can be only one notification per page load. The last one wins and overwrites the other ones.