Webhook - success message to the panel

How can I send a message to the panel after a successful hook?

I tried that:

'page.update:after' => function ($newPage, $oldPage) 
{
  try{
    if($newPage->title()== "Poster"):
      exec("wkhtmltopdf https:/magermilch.cc/de/poster content/poster/poster.pdf");
    endif;
    $panel()->notify('the pdf was created');

  } catch(Exception $e) 
  {

    $e->getMessage();

  }
       
}

greetings perry

You can only throw an exception from a hook, there’s no such thing as panel()->notify() in K3.

is an exeption an error message ?

The exception pops up a message that can contain anything:

throw new Exception('hey, congrats, your poster was successfully saved as PDF');

The user can then dismiss this message.

1 Like

like this ?

   page.update:after' => function ($newPage, $oldPage) 
        {
          try{
            if($newPage->title()== "Poster"):
              exec("wkhtmltopdf https://magermilch.cc/de/poster content/poster/poster.pdf");
            endif;
            throw new Exception('hey, congrats, your poster was successfully saved as PDF');


          } catch(Exception $e) 
          {

            $e->getMessage();

          }
               
        }

Yes, that should work.

should this above still work?
I think throw new Exception() is making the code to die, correct?

Kirby::plugin('my/test-plugin', [
    'hooks' => [
        'page.update:after' => function ($newPage, $oldPage) {
            
            throw new Exception('saved!');
            
        }
    ]
]);

is actually not working, it is showing the modal “saved!” but does not save.
I am looking for hours. Is there a way in kirby 4 to build a plugin, with a hook and give sort of feedback to the user with a notification, an alert, a modal?

For all I can see this works, but the exceptions stops the “Save” button from disappearing. The actual saving happens before the :after hook is called.

What exactly are you trying to achieve with your plugin?

I am getting the geo coordinates of an adress.


use Kirby\Cms\App;

App::plugin('my/geolocation', [
    'hooks' => [
        'page.update:after' => function ($newPage, $oldPage) {
            getCoordinatesIfNeeded($newPage);
        }
    ]
]);

function getCoordinatesIfNeeded($page) {
    
    if ($page->parent() && $page->parent()->slug() === 'orte') {
        
        if ($page->long()->isEmpty() && $page->lati()->isEmpty()) {
            $strasse = $page->strasse()->value();
            $plz = $page->plz()->value();

            if (!empty($strasse) && !empty($plz)) {
                $address = $strasse . ', ' . $plz;

                // Make API request to OpenCageData
                $apiKey = 'XXX'; 
                $url = 'https://api.opencagedata.com/geocode/v1/json?q=' . urlencode($address) . '&key=' . $apiKey;

                // Use file_get_contents or cURL for API request
                $response = @file_get_contents($url);
                if ($response === false) {
                    return;
                }

                $data = json_decode($response, true);

                if (isset($data['results'][0]['geometry'])) {
                    $latitude = $data['results'][0]['geometry']['lat'];
                    $longitude = $data['results'][0]['geometry']['lng'];

                    // Update the page with the lat/long values
                    $page->update([
                        'lati' => $latitude,
                        'long' => $longitude
                    ]);
                }
            }
        }
    }
}

It works very well and magicly! BUT the values are not inserted into the input fields directly, I have to go back to the parent page and open again (or just refresh). Probably because it´s the update:after hook. It would be great if I could refresh or go back to parent automaticly. But I don´t know how.

And I wanted to show a notification: “Found geo data!” / “Did not found geo data!”

After reading more posts here in the forum, it looks like the notification is not that easy.

If you have a hint for the refresh/redirection or filling the input with the values from geo API, let me know. Is there an event so I could fill in the values with JS after saving? Thank you for the help!