statamic / cms

The core Laravel CMS Composer package
https://statamic.com
Other
4.03k stars 530 forks source link

SubmissionSaving and SubmissionCreated Event is not triggered #10926

Closed thomas4Bitcraft closed 3 weeks ago

thomas4Bitcraft commented 3 weeks ago

Bug description

I wanted to use the two Event Listeners, but they are not triggering.

How to reproduce

Here is the implementation:

class AppServiceProvider extends ServiceProvider
{

    protected $listen = [
        SubmissionSaving::class => [FormSaving::class],
        SubmissionCreated::class => [FormSubmission::class],
    ];
    ...
}
class FormSaving
{
    /**
     * @param SubmissionSaving $event
     * @return void
     */
    public function handle(SubmissionSaving $event)
    {
        dd("test");
     }
}

When I send a POST request to http://<domain>/!/forms/<form_handle> this is not triggered

Logs

No response

Environment

Environment
Application Name: xxx
Laravel Version: 11.23.5
PHP Version: 8.2.7
Composer Version: 2.7.7
Environment: local
Debug Mode: ENABLED
URL: xxx
Maintenance Mode: OFF
Timezone: UTC
Locale: en

Cache
Config: NOT CACHED
Events: NOT CACHED
Routes: NOT CACHED
Views: CACHED

Drivers
Broadcasting: log
Cache: file
Database: mysql
Logs: stack / single
Mail: smtp
Queue: sync
Session: file

Sentry
Enabled: YES
Environment: local
Laravel SDK Version: 4.8.0
PHP SDK Version: 4.9.0
Release: NOT SET
Sample Rate Errors: 100%
Sample Rate Performance Monitoring: 0%
Sample Rate Profiling: NOT SET
Send Default PII: DISABLED

Statamic
Addons: 4
Sites: 2 (DE, EN)
Stache Watcher: Disabled
Static Caching: Disabled
Version: 5.30.0 PRO

Statamic Addons
bitcraft/form-webhook: dev-master
bitcraft/page-generator: dev-master
statamic/eloquent-driver: 4.14.3
stillat/relationships: 2.2.1

Statamic Eloquent Driver
Asset Containers: eloquent
Assets: eloquent
Blueprints: eloquent
Collection Trees: eloquent
Collections: eloquent
Entries: eloquent
Forms: eloquent
Global Sets: eloquent
Global Variables: eloquent
Navigation Trees: eloquent
Navigations: eloquent
Revisions: eloquent
Sites: file
Taxonomies: eloquent
Terms: eloquent
Tokens: eloquent

Installation

Fresh statamic/statamic site via CLI

Additional details

No response

duncanmcclean commented 3 weeks ago

Is this app using the new Laravel 11 skeleton, or is it using the old one? The easiest way is to check if your bootstrap/app.php file contains Application::configure().

If you are using the new app skeleton, it looks like you're doing everything correctly. Can you share the contents of your bootstrap/app.php and app/Providers/AppServiceProvider.php 🤔

If you're not using the new app skeleton, I think it's not working because you've added the $listen property to the AppServiceProvider, instead of the EventServiceProvider.

thomas4Bitcraft commented 3 weeks ago

@duncanmcclean Yes I'm using the new app.php:

<?php

use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
use Sentry\Laravel\Integration;

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__ . '/../routes/web.php',
        commands: __DIR__ . '/../routes/console.php',
        health: '/up',
    )
    ->withMiddleware(function (Middleware $middleware) {
        $middleware->validateCsrfTokens(except: [
            '/generate/*',
            '/!/forms/*',
        ]);
        $middleware->append([
            \App\Http\Middleware\LocaleMiddleware::class,
        ]);
    })
    ->withExceptions(function (Exceptions $exceptions) {
        Integration::handles($exceptions);
    })->create();

And set the $listen poperty in the AppServiceProvider.php

<?php

namespace App\Providers;

use Bitcraft\FormWebhook\FormSaving;
use Bitcraft\FormWebhook\FormSubmission;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\ServiceProvider;
use Statamic\Events\SubmissionCreated;
use Statamic\Events\SubmissionSaving;
use Statamic\Facades\Collection;
use Statamic\Statamic;
use Stillat\Relationships\Support\Facades\Relate;

class AppServiceProvider extends ServiceProvider
{

    protected $listen = [
        SubmissionSaving::class => [FormSaving::class],
        SubmissionCreated::class => [FormSubmission::class],
    ];

    /**
     * Register any application services.
     */
    public function register(): void
    {
        //
    }

    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
        if (env('APP_ENV') !== 'local') {
            URL::forceScheme('https');
        }

        Relate::manyToMany(
            'videolearnings.videos',
            'videos.video_learning'
        );

        Collection::computed('posts', 'category_slug', function ($entry, $value) {
            return $entry->category?->slug();
        });

        Statamic::vite('app', [
            'resources/css/cp.css',
            'resources/js/cp.js',
        ]);
    }
}
duncanmcclean commented 3 weeks ago

You don't need to manually register event listeners in service providers anymore, as long as they live in app/Listeners.

CleanShot 2024-10-10 at 14 23 13

Have you imported the SubmissionSaving event in your listener classes?

use Statamic\Events\SubmissionSaving;
duncanmcclean commented 3 weeks ago

In fact, looking at the code you provided, the events you had imported in your AppServiceProvider were events belonging to a third-party addon, not Statamic.

Make sure you're importing the correct events.

thomas4Bitcraft commented 3 weeks ago

Only the listeners itself are imported from an addon I wrote. The Events itself are imported from Statamic\Events\SubmissionSaving.

This is the class for the Event Listener:

<?php

namespace Bitcraft\FormWebhook;

use Statamic\Events\SubmissionSaving;

class FormSaving
{
    /**
     * @param SubmissionSaving $event
     * @return void
     */
    public function handle(SubmissionSaving $event)
    {
        dd("asd");
    }
}

I added the Listeners to App/Listeners now and it works - weird that it does not when defining them manually

duncanmcclean commented 3 weeks ago

Ohhh, it's inside an addon.

Listeners inside addons aren't automatically registered yet, you need to register them manually as per the addon docs.

It's coming soon though: #10911