Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
geertjanknapen1 opened this issue Aug 14, 2024 · 1 comment
Labels

Comments

@geertjanknapen1
Copy link

geertjanknapen1 commented Aug 14, 2024

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:

  • Am I doing something wrong? Can I not call Toast Notifications from the Event Listener?
  • Is it because I'm listening for the wrong event? Should it be AssetCreated or AssetSaved or something?
  • Is it because it is happening on the Asset part of CP and can I only call Toast Notifications from other places? (Like entry creation or a custom utility?)
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:

  • Create an asset container.
  • Create an event listener that listens for the AssetUploaded event.
  • In the code, call a Toast notification using the Toast facade. (call it without utilizing an exception)
  • Upload an asset to the asset container.
  • See that nothing happens in CPanel regarding a custom Toast notification.

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 geertjanknapen1 changed the title Toast fired using the facade are not displaying. Toast fired using the facade is not displaying. Aug 14, 2024
@geertjanknapen1
Copy link
Author

geertjanknapen1 commented Aug 14, 2024

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

@duncanmcclean duncanmcclean changed the title Toast fired using the facade is not displaying. Toast fired in an AssetUploaded listener using the facade is not displaying. Aug 14, 2024
@duncanmcclean duncanmcclean changed the title Toast fired in an AssetUploaded listener using the facade is not displaying. Toasts fired in an AssetUploaded event listener using the facade are not displaying Aug 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants