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

Duplicate field error on multi-site #4853

Open
aerni opened this issue Dec 3, 2021 · 1 comment
Open

Duplicate field error on multi-site #4853

aerni opened this issue Dec 3, 2021 · 1 comment

Comments

@aerni
Copy link
Contributor

aerni commented Dec 3, 2021

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: #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

  • Create a multi-site and configure a collection to use at least two sites
  • Set the blueprint content on EntryBlueprintFound
  • Visit an entry in the CP
  • See it throw the duplicate fields error

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
Copy link
Contributor Author

aerni commented Dec 4, 2021

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');
}

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

No branches or pull requests

2 participants