Skip to content

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

@adampatterson

Description

@adampatterson

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions