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

Event::listen callback on EntrySaved breaks AssetReferenceUpdater #7352

Closed
stuartcusackie opened this issue Jan 14, 2023 · 2 comments
Closed
Labels

Comments

@stuartcusackie
Copy link

stuartcusackie commented Jan 14, 2023

Bug description

Defining simple callback based event listeners in EventServiceProvider break the AssetReferenceUpdater, e.g.

app/Providers/EventServiceProvider.php

public function boot()
{
  // Adding this breaks asset moving
  // "Undefined array key listener"
  Event::listen(function (EntrySaved $event) {
    \Log::info('EntrySaved. Handle: ' . $event->entry->collectionHandle());
   });
}

If you add the above code then:

  1. Add an asset to an entry
  2. Move / rename that asset
  3. You will see the "Undefined array key listener" error.
  4. The asset will in fact be moved but any entry reference updates will fail.

The strange thing is that using a Listener class instead of a callback works fine.
It seems that a few different entry / nav events are causing this behaviour (not just EntrySaved)

I have made a repo to demonstrate this:
https://github.com/stuartcusackie/stateventtest

How to reproduce

Download this repo and follow the instructions in README:
https://github.com/stuartcusackie/stateventtest

Logs

[2023-01-14 11:30:35] local.ERROR: Undefined array key "listener" {"userId":"8ab59baf-98f6-4737-832d-7aa1a06a681b","exception":"[object] (ErrorException(code: 0): Undefined array key \"listener\" at C:\\webdev\\laragon\\stateventtest\\vendor\\statamic\\cms\\src\\Events\\Subscriber.php:86)
[stacktrace]
#0 C:\\webdev\\laragon\\stateventtest\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Bootstrap\\HandleExceptions.php(266): Illuminate\\Foundation\\Bootstrap\\HandleExceptions->handleError(2, 'Undefined array...', 'C:\\\\webdev\\\\larag...', 86)
#1 C:\\webdev\\laragon\\stateventtest\\vendor\\statamic\\cms\\src\\Events\\Subscriber.php(86): Illuminate\\Foundation\\Bootstrap\\HandleExceptions->Illuminate\\Foundation\\Bootstrap\\{closure}(2, 'Undefined array...', 'C:\\\\webdev\\\\larag...', 86)
#2 C:\\webdev\\laragon\\stateventtest\\vendor\\statamic\\cms\\src\\Providers\\EventServiceProvider.php(47): Statamic\\Events\\Subscriber::normalizeRegisteredListener(Object(Closure))
#3 [internal function]: Illuminate\\Events\\Dispatcher->Statamic\\Providers\\{closure}(Object(Closure), 3)
#4 C:\\webdev\\laragon\\stateventtest\\vendor\\laravel\\framework\\src\\Illuminate\\Collections\\Arr.php(795): array_filter(Array, Object(Closure), 1)
#5 C:\\webdev\\laragon\\stateventtest\\vendor\\statamic\\cms\\src\\Providers\\EventServiceProvider.php(48): Illuminate\\Support\\Arr::where(Array, Object(Closure))
#6 C:\\webdev\\laragon\\stateventtest\\vendor\\laravel\\framework\\src\\Illuminate\\Macroable\\Traits\\Macroable.php(124): Illuminate\\Events\\Dispatcher->Statamic\\Providers\\{closure}('Statamic\\\\Events...', 'Statamic\\\\Git\\\\Su...')
#7 C:\\webdev\\laragon\\stateventtest\\vendor\\statamic\\cms\\src\\Events\\Subscriber.php(49): Illuminate\\Events\\Dispatcher->__call('forgetListener', Array)
#8 C:\\webdev\\laragon\\stateventtest\\vendor\\statamic\\cms\\src\\Events\\Subscriber.php(68): Statamic\\Events\\Subscriber::disable()
#9 C:\\webdev\\laragon\\stateventtest\\vendor\\statamic\\cms\\src\\Data\\DataReferenceUpdater.php(287): Statamic\\Events\\Subscriber::withoutListeners(Object(Closure))
#10 C:\\webdev\\laragon\\stateventtest\\vendor\\statamic\\cms\\src\\Data\\DataReferenceUpdater.php(66): Statamic\\Data\\DataReferenceUpdater->saveItem()
#11 C:\\webdev\\laragon\\stateventtest\\vendor\\statamic\\cms\\src\\Listeners\\UpdateAssetReferences.php(99): Statamic\\Data\\DataReferenceUpdater->updateReferences('rust1.jpg', 'test/rust1.jpg')
#12 [internal function]: Statamic\\Listeners\\UpdateAssetReferences->Statamic\\Listeners\\{closure}(Object(Statamic\\Entries\\Entry), 0)
#13 C:\\webdev\\laragon\\stateventtest\\vendor\\laravel\\framework\\src\\Illuminate\\Collections\\Arr.php(560): array_map(Object(Closure), Array, Array)
#14 C:\\webdev\\laragon\\stateventtest\\vendor\\laravel\\framework\\src\\Illuminate\\Collections\\Collection.php(739): Illuminate\\Support\\Arr::map(Array, Object(Closure))
#15 C:\\webdev\\laragon\\stateventtest\\vendor\\statamic\\cms\\src\\Listeners\\UpdateAssetReferences.php(100): Illuminate\\Support\\Collection->map(Object(Closure))
#16 C:\\webdev\\laragon\\stateventtest\\vendor\\statamic\\cms\\src\\Listeners\\UpdateAssetReferences.php(48): Statamic\\Listeners\\UpdateAssetReferences->replaceReferences(Object(Statamic\\Assets\\Asset), 'rust1.jpg', 'test/rust1.jpg')
#17 C:\\webdev\\laragon\\stateventtest\\vendor\\laravel\\framework\\src\\Illuminate\\Events\\CallQueuedListener.php(107): Statamic\\Listeners\\UpdateAssetReferences->handleSaved(Object(Statamic\\Events\\AssetSaved))
#18 C:\\webdev\\laragon\\stateventtest\\vendor\\laravel\\framework\\src\\Illuminate\\Container\\BoundMethod.php(36): Illuminate\\Events\\CallQueuedListener->handle(Object(Illuminate\\Foundation\\Application))
#19 C:\\webdev\\laragon\\stateventtest\\vendor\\laravel\\framework\\src\\Illuminate\\Container\\Util.php(41): Illuminate\\Container\\BoundMethod::Illuminate\\Container\\{closure}()
#20 C:\\webdev\\laragon\\stateventtest\\vendor\\laravel\\framework\\src\\Illuminate\\Container\\BoundMethod.php(93): Illuminate\\Container\\Util::unwrapIfClosure(Object(Closure))
#21 C:\\webdev\\laragon\\stateventtest\\vendor\\laravel\\framework\\src\\Illuminate\\Container\\BoundMethod.php(37): Illuminate\\Container\\BoundMethod::callBoundMethod(Object(Illuminate\\Foundation\\Application), Array, Object(Closure))
#22 C:\\webdev\\laragon\\stateventtest\\vendor\\laravel\\framework\\src\\Illuminate\\Container\\Container.php(663): Illuminate\\Container\\BoundMethod::call(Object(Illuminate\\Foundation\\Application), Array, Array, NULL)
#23 C:\\webdev\\laragon\\stateventtest\\vendor\\laravel\\framework\\src\\Illuminate\\Bus\\Dispatcher.php(128): Illuminate\\Container\\Container->call(Array)
#24 C:\\webdev\\laragon\\stateventtest\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(141): Illuminate\\Bus\\Dispatcher->Illuminate\\Bus\\{closure}(Object(Illuminate\\Events\\CallQueuedListener))
#25 C:\\webdev\\laragon\\stateventtest\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php(116): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Illuminate\\Events\\CallQueuedListener))
#26 C:\\webdev\\laragon\\stateventtest\\vendor\\laravel\\framework\\src\\Illuminate\\Bus\\Dispatcher.php(132): Illuminate\\Pipeline\\Pipeline->then(Object(Closure))

...

Environment

Version: 3.3.64 Solo

Installation

Fresh statamic/statamic site via CLI

Antlers Parser

None

Additional details

No response

@ryanmitchell
Copy link
Contributor

This seems to work ok for me on the latest version of Statamic.
Are you still experiencing the issue on 4.36.0?

@jasonvarga
Copy link
Member

Yeah this is not an issue on modern versions of Statamic/Laravel.

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

4 participants