Show notification message after model update (from config.php)

Hi,

I’m generating “lessons” by clicking a controlpanel button (new in v5), executing my model. The concept works, my model generates the lessons. But I’d like to show a notification message saying “lessons created”! Whatever I try (with my AI friends (:slight_smile: I cannot make the message appear in the control panel.

below my config.php with the code.

Any help is welcome…

<?php
// site/config/config.php
//
// UPDATED: Using a full Response object for the redirect, which is more reliable.
?>
<?php

return [
    'debug' => true,
    'routes' => [
        [
            'pattern' => '(:all)/generate-lessons',
            'action'  => function (string $slug) {
                $kirby = kirby();
                $page = page($slug);

                if (!$page || $page->intendedTemplate()->name() !== 'lessongroup') {
                    return false;
                }

                if ($page->lessonsGenerated()->isTrue()) {
                    $message = 'Lessons have already been generated. To regenerate, turn off the toggle and save first.';
                    $kirby->session()->set('kirby.info', $message);
                    return Kirby\Http\Response::redirect($page->panel()->url());
                }

                try {
                    // Step 1: Generate lessons and get the count.
                    $count = $page->generateLessons();

                    // Step 2: Manually update the page toggle. This is the action.
                    $page->update(['lessonsGenerated' => true]);

                    // Step 3: Set the success message in the session.
                    $message = "{$count} lessons were successfully generated for '{$page->description()}'";
                    $kirby->session()->set('kirby.success', $message);
                    
                    // Step 4: Redirect back to the panel using the Response object.
                    return Kirby\Http\Response::redirect($page->panel()->url());

                } catch (Exception $e) {
                    $kirby->session()->set('kirby.error', $e->getMessage());
                    return Kirby\Http\Response::redirect($page->panel()->url());
                }
            }
        ]
    ]
];

There is currently no way to trigger a notification message in the Panel from the PHP side, only in your JavaScript plugin code (if you have any). The JS docs for notifications:
https://lab.getkirby.com/public/lab/internals/panel/notification

There is an idea thought to add this feature which you could upvote: https://feedback.getkirby.com/669

Thanks for your quick respons, saves me a ton of searching!!

Have a great day!