statamic / cms

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

Toasts fired in an `AssetUploaded` event listener using the facade are not displaying #10626

Open geertjanknapen1 opened 1 month ago

geertjanknapen1 commented 1 month ago

Bug description

The documentation states I should be able to fire Toast notifications from the server using the Toast facade.

Based on when an asset is uploaded, I want to create an entry in a specific collection. This entry should have a "sanitized" version of the asset title. So I listen to the AssetUploaded event, and if it is fired for a specific asset container I get to work creating an entry.

However, we will not be listening for AssetDeleted events, so in my event listener, I check if an entry with that specific "sanitized" name already exists. If that happens, I DO NOT want to cancel uploading of the asset, instead, I want to show a Toast notification to the user, giving them some info that the entry was not automatically created.

So I followed the documentation and utilized the code below to show a Toast notification. However, except for the X Asset uploaded toast, nothing displays.

So my questions are:

Toast::error(
    "Download Entry with name '{$entryName}' already exists. Download entry was not automatically created for '{$filename}', please notify the content department of the document."
)
->duration(5000);

I'll post the full handle method of the listener below.

public function handle(AssetUploaded $event): void
{
    $asset = $event->asset;

    // Only handle assets from the 'TDS Documents' container.
    if ($asset->container()->handle === Disk::TDS_DOCUMENTS->value) {
        $filename = $asset->path();
        $entryName = $this->extractEntryNameFromAssetName($filename); // This just strips extension and '-' from asset name.

        if (!is_null(Entry::whereCollection(Collection::DOWNLOADS->value)->where('title', $entryName)->first())) {
            Toast::error(
                "Download Entry with name '{$entryName}' already exists. Download entry was not automatically created for '{$filename}', please notify the content department of the new document."
            )
            ->duration(5000);
        }

        // Create the replicator set for this TDS document.
        $replicatorSetData = [
            'id' => str_random(8),
            'download_language' => Locale::ENGLISH->value,
            'file' => $filename,
            'type' => 'download',
            'enables' => true,
        ];

        // Create the array of data for the Entry.
         $entryData = [
            'title' => $entryName,
            'file' => $filename,
            'download_category' => [
                str_replace('_', '-', DownloadCategory::TDS_DOCUMENT->value)
            ],
            'download_categories' => [
                DownloadCategory::TDS_DOCUMENT->value
            ],
            'download_template' => DownloadTemplate::TDS->value,
            'utilise_download_form' => false,
            'multiple_tds' => [
                $replicatorSetData
            ],
            'thumbnail_small' => [
                'src' => ''
            ]
        ];

        // Create the Entry itself on the correct collection.
        Entry::make()
            ->collection(Collection::DOWNLOADS->value)
            ->slug(str_slug($entryName))
            ->published(true)
            ->blueprint('download')
            ->data($entryData)
            ->save();
        }
}

How to reproduce

Minimal steps would be as follows:

Code for it could look as follows:

public function handle(AssetUploaded $event): void
{
    Toast::error(
        "Download Entry with name '{$entryName}' already exists. Download entry was not automatically created for '{$filename}', please notify the content department of the new TDS file."
    )
    ->duration(5000);
}

Logs

No response

Environment

Environment
Application Name: mysite
Laravel Version: 10.48.4
PHP Version: 8.3.1
Composer Version: 2.6.6
Environment: local
Debug Mode: ENABLED
URL: mysite.test
Maintenance Mode: OFF

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

Drivers
Broadcasting: log
Cache: statamic
Database: mysql
Logs: stack / daily, stderr
Mail: smtp
Queue: redis
Session: file

Statamic
Addons: 5
Antlers: runtime
Sites: 8 (English, Dutch, Spanish, and 5 more)
Stache Watcher: Disabled
Static Caching: half
Version: 4.55.0 PRO

Statamic Addons
appswithlove/statamic-one-click-content-translation: 4.63.0
aryehraber/statamic-impersonator: 2.6.0
spatie/statamic-responsive-images: 4.1.1
statamic-rad-pack/meilisearch: 3.2.1
withcandour/aardvark-seo: 3.0.1

Installation

Existing Laravel app

Additional details

No response

geertjanknapen1 commented 1 month ago

Update to clarify. With " I DO NOT want to cancel uploading of the asset, instead, I want to show a Toast notification to the user, giving them some info that the entry was not automatically created." I mean I still want the X Asset uploaded toast, but another custom toast with information.

Not what is shown below. I know this does not cancel uploading of assets, but it definitely looks like it for the end-user.

image