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

Fix custom notification object #9311

Merged
merged 5 commits into from
Oct 27, 2023

Conversation

wychoong
Copy link
Contributor

  • Changes have been thoroughly tested to not break existing functionality.
  • New functionality has been documented or existing documentation has been updated to reflect changes.
  • Visual changes are explained in the PR description using a screenshot/recording of before and after.

closes #9268

when recreating the notification object from array, Notification class resolved the correct CustomNotification but the CustomNotification::fromArray is not called again. this PR fixes that by checking if the notification object resolved from container is custom and its called from Notification class to ensure its only called once and prevent infinite loop

another approach would be in livewire\Notifications to resolved the notification object but that would have multiple useless instance created and more code changes that I not able to test

@danharrin
Copy link
Member

Duplicate of #9299?

@wychoong
Copy link
Contributor Author

Duplicate of #9299?

Fixed the same issue without exposing unnecessary info

@danharrin
Copy link
Member

I don't think I understand how it works?

@wychoong
Copy link
Contributor Author

wychoong commented Oct 26, 2023

I don't think I understand how it works?

in this file

public function pullNotificationsFromSession(): void
{
foreach (session()->pull('filament.notifications') ?? [] as $notification) {
$notification = Notification::fromArray($notification);
$this->pushNotification($notification);
}
}

Notification::fromArray($notification) is Filament\Notifications\Notification

here it will return the CustomNotification from the make method

public static function fromArray(array $data): static
{
$static = static::make($data['id'] ?? Str::random());
$static->actions(

and it continues to configure the object with the array data, thus it never gets to call to CustomNotification::fromArray, this can be verified by dd in CustomNotification::fromArray

with this fix

if (is_subclass_of($static::class, self::class) && get_called_class() === self::class) {
    $static = $static::fromArray($data);
}

it checks if the $static notification class is extended from the base Notification.
And also make sure it is called from Notification. This is because we need to call parent::fromArray in the CustomNotification to ensure the rest of properties are set.
Without these checks it will be infinite loops as base->custom->base->custom->...

@zepfietje zepfietje added the bug Something isn't working label Oct 27, 2023
@zepfietje zepfietje added this to the v3 milestone Oct 27, 2023
@danharrin danharrin merged commit 09b513e into filamentphp:3.x Oct 27, 2023
@danharrin
Copy link
Member

Thanks!

@wychoong wychoong deleted the fix-custom-notification-object branch October 27, 2023 12:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Custom notifications do not persist state
3 participants