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

Modifying Entry taxonomy data using an event listener causes an error in RelationshipFieldtype #11396

Open
adampatterson opened this issue Jan 27, 2025 · 7 comments · May be fixed by #11356
Open

Comments

@adampatterson
Copy link

adampatterson commented Jan 27, 2025

Bug description

Modifying an entries taxonomy data using an Event Listener causes errors in the control panel.

I'm using the Eloquent driver although I've tested this with a fresh flat file install as well and it's actually worse.

I'm using Revisions, but the problem exists when it's not enabled.

Save and Edit needs to be enabled, Save and Go To Listing doesn't result in the issue.

I'm syncing another entries taxonomies with the entry that's being edited. This is done through EventSaved or EventSaving.

Image

Editing content, titles, or other regular fields through the Listeners don't result in this problem.

How to reproduce

  • In the control panel, I add link a Structure Entry to a Listing Entry
  • This is represented as an ID, so an array of IDs.
  • When the Entry is saved, a revision is created
    • contains the Structure Entry IDs.
  • When the Listing Entry is published an EventListener takes the Structure IDs and returns the taxonomy + terms from those Structure entries collections.
  • Those taxonomies and terms are then applied the Listing Entry
    • Data saved to the database and is correct
  • However the Terms displayed in the control panel are broken.
  • Reloading the page fixes the issue.

I've tested with and without revisions
EventSave and EventSaving

If the error shows, then refresh the page, this really boils down to modifying the taxonomy data.

    public function handle(EntrySaving $event): void
    {
        $entry = $event->entry;

        if ($event->entry->isDirty('structure')
            && $entry->collection()->handle() === Collections::LISTINGS->value
        ) {
            $entry->set('taxonomy_one', 'term-one'); 
     
            $entry->saveQuietly();
        }
    }

It's worse when using flat files since it happens on create.

The Database version on create is fine, but when you remove the link and terms then save revisions the EventSaving/Save fires again for some reason.

I've used the Vue inspector and can see that the data on the in RelationshipFieldtype represents the error above.
Reloading the page, represents the data correctly.

Logs

Environment

Environment
Application Name: Statamic
Laravel Version: 11.37.0
PHP Version: 8.2.27
Composer Version: 2.8.1
Environment: adam
Debug Mode: ENABLED
URL: tourism-calgary.adam.paperleaf.dev
Maintenance Mode: OFF
Timezone: America/Edmonton
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: log
Queue: sync
Session: file

Sentry
Enabled: MISSING DSN
Environment: adam
Laravel SDK Version: 4.10.2
PHP SDK Version: 4.10.0
Release: NOT SET
Sample Rate Errors: 100%
Sample Rate Performance Monitoring: NOT SET
Sample Rate Profiling: NOT SET
Send Default PII: DISABLED

Statamic
Addons: 4
Sites: 1
Stache Watcher: Disabled (auto)
Static Caching: Disabled
Version: 5.46.0 PRO

Statamic Addons
statamic/eloquent-driver: 4.19.1
statamic/importer: 1.7.2
statamic/seo-pro: 6.5.0
thoughtco/statamic-restrict-fields: 1.1.0

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: eloquent
Taxonomies: eloquent
Terms: eloquent
Tokens: eloquent

Installation

Fresh statamic/statamic site via CLI

Additional details

I've only tested this with Taxonomies.

@duncanmcclean
Copy link
Member

You've shared some example code, which looks fine to me. What does your actual code look like? Are you adding multiple terms, or just one?

@adampatterson
Copy link
Author

adampatterson commented Jan 28, 2025

Hey @duncanmcclean,

My actual use case would add multiple terms if if there are any.

I have 2 fresh installs one using Eloquent and the other Flat Files.

I can push a Repo if you'd like.

 public function handle(EntrySaving $event): void
{
    $entry = $event->entry;

    $entry->set('taxonomy_one', 'one'); ❌
    $entry->set('taxonomy_three', ['term-two', 'term-two-two']); ❌
    $entry->set('taxonomy_three', collect(['term-three', 'term-three-three'])); ❌

    $entry->saveQuietly();

}

Image

I tested with a multi select and have a similar experience.

$entry->set('select', 'one'); ✅
$entry->set('select', collect(['term-three', 'term-three-three'])); ❌

Image

Reloading the page also fixes the issue.


I have also noticed that if the entry has not been published EventSaved is called and likely updated the Entry rather than a Revision. I don't think that's unexpected.


Another observation, is that if I update one taxonomy, all of them will display incorrectly.

@duncanmcclean
Copy link
Member

Thanks! I was able to reproduce it. Looks like it'll be fixed by #11356.

@duncanmcclean
Copy link
Member

Also, you don't need to call ->saveQuietly(); in the EntrySaving listener as it happens before the entry gets saved.

@adampatterson
Copy link
Author

@duncanmcclean Thanks! I was originally using EntrySaved which did. Forgot to update in the testing examples.

Would there be an ETA on that pull request?

I can reload the page for now while we're in development.

@duncanmcclean
Copy link
Member

I don't have an ETA on it, sorry!

@adampatterson
Copy link
Author

@duncanmcclean no problem, we're still in development for a few months. I'd love this resolved before the client begins entering content. But we can work around that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants