statamic / cms

The core Laravel CMS Composer package
https://statamic.com
Other
3.71k stars 508 forks source link

Duplicate field error on multi-site #4853

Open aerni opened 2 years ago

aerni commented 2 years ago

Bug description

I've got a subscriber that extends the blueprint of every entry on EntryBlueprintFound.

I'm running into a Duplicate field [seo_section_title_description] on blueprint [pages]. This is the same issue I talked about here: https://github.com/statamic/cms/issues/4286. The workaround we came up with comes with its own problems. Which is why I dug a little deeper so I can confidently reproduce the initial issue.

The problem only seems to happen on multi-sites. On multi-sites, the EntryBlueprintFound event gets triggered for every locale of an entry. So if you add to the blueprint in the event like $event->blueprint->setContents($contents), it will throw the exception the second time this method is called.

The simple workaround was to use ensureFieldsInSection() instead. But the problem with this method is that it ensures all the fields for every locale. This wouldn't be an issue per se, as it clears out duplicate fields when it renders the blueprint. But in my case, it is a problem. That's because I need to add different default values to the blueprint fields for every locale. And because any duplicate ensured fields get stripped out, I end up with the wrong fields. I hope this makes sense.

How to reproduce

Logs

No response

Versions

Statamic 3.2.24 Pro Laravel 8.74.0 PHP 8.0.13

Installation

Fresh statamic/statamic site via CLI

Additional details

No response

aerni commented 2 years ago

I've got my problem solved with this workaround. But it feels very yacky.

/**
* Add the fields to the entry blueprint in the CP. But only for the current localized entry.
* This is to prevent that every localization adds fields to the blueprint.
* If we don't do this check, we can't add fields based on entry localization correctly.
*/
if (Str::containsAll(request()->path(), [$event->entry?->id(), config('cp.route', 'cp'), 'collections', 'entries'])) {
    $event->blueprint->ensureFieldsInSection($this->blueprint($event)->items(), 'SEO');
}