statamic / cms

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

Bard fieldtype: Linked entries in Bard field resolves to wrong locale of the entry (GraphQL and RESTful API) #9424

Open motion-work opened 9 months ago

motion-work commented 9 months ago

Bug description

I have a Statamic instance configured with multisite (German, French, and Italian). The default root locale is set to German. Also I am fetching the CMS data via the GraphQL endpoint. the same bug applies for the REST api as well.

Within a bard field, when I link to an entry of the same locale - for example, French entry to another French entry, I expect the linked entry to resolve to the French URL /fr/first-page -> /fr/second-page, but it currently does not.

How to reproduce

Current behavior

French linked entry within Bard field resolves to German url of the linked entry. for example:

/fr/french-page-a -> /de/german-page-a

it resolves to the correct entry but in the wrong locale

Expected behavior

/fr/french-page-a -> /fr/french-page-a

Screenshot 2024-01-31 at 11 21 07

Logs

No response

Environment

Environment
Laravel Version: 9.52.16
PHP Version: 8.2.13
Composer Version: 2.5.5
Environment: local
Debug Mode: ENABLED
URL: app.url
Maintenance Mode: OFF

Cache
Config: NOT CACHED
Events: NOT CACHED
Routes: NOT CACHED
Views: CACHED

Drivers
Broadcasting: log
Cache: redis
Database: mysql
Logs: stack / daily, sentry
Mail: smtp
Queue: sync
Session: redis

Sentry
Enabled: MISSING DSN
Environment: local
Laravel SDK Version: 3.8.2
PHP SDK Version: 3.22.1
Release: e6c02a7f0
Sample Rate Errors: 100%
Sample Rate Performance Monitoring: 100%
Sample Rate Profiling: NOT SET
Send Default PII: ENABLED

Statamic
Addons: 4
Antlers: regex
Stache Watcher: Enabled
Static Caching: Disabled
Version: 4.46.0 PRO

Installation

Fresh statamic/statamic site via CLI

Antlers Parser

None

Additional details

I have found this merged issue which seems to fix this similar bug but not for the GraphQL and REST api: https://github.com/statamic/cms/pull/8319

motion-work commented 9 months ago

any updates on this? 😬

jasonvarga commented 9 months ago

No updates yet sorry. You'd see them here if there were.

motion-work commented 9 months ago

alrighty thanks for the reply

jasonvarga commented 9 months ago

I'm not able to fix it right now, but this is a note for us or whoever wants to tackle this:

The issue is here: https://github.com/statamic/cms/blob/1f08790aa9b53325c1f4a4bd4ee91507b95362ad/src/Fieldtypes/Bard/LinkMark.php#L69

It's localizing to the current site, which works great on the frontend but not in the APIs. Instead of Site::current(), it should somehow get the site of the parent entry (the one where the bard field is). Accessing that from within the LinkMark class might be challenging.

arcs- commented 1 month ago

In my project I "fixed" this by injecting a middleware into the Statamic GraphQL queries

class ResolveSite extends Middleware
{
    public function handle($root, array $args, $context, ResolveInfo $info, Closure $next)
    {
        $site = $args['site'] ?? Site::default()->handle();
        Site::setCurrent($site);

        return $next($root, $args, $context, $info);
    }
}

This solution isn't that nice because of it being a side-effect in a query, however it does work quite well even with fetching multiple sites, and doesn't require refactoring all the Site::current() occurrences.

G-Nat commented 2 days ago

Currently facing same issue with REST API 😞