diff --git a/src/Fieldtypes/Bard/Augmentor.php b/src/Fieldtypes/Bard/Augmentor.php index 4d1db607d80..b1dc686c889 100644 --- a/src/Fieldtypes/Bard/Augmentor.php +++ b/src/Fieldtypes/Bard/Augmentor.php @@ -13,6 +13,7 @@ class Augmentor { + public static $currentBardConfig = []; protected $fieldtype; protected $sets = []; protected $includeDisabledSets = false; @@ -169,7 +170,13 @@ public function renderHtmlToProsemirror(string $value) public function renderProsemirrorToHtml(array $value) { - return $this->editor()->setContent($value)->getHTML(); + static::$currentBardConfig = $this->fieldtype->config(); + + $html = $this->editor()->setContent($value)->getHTML(); + + static::$currentBardConfig = []; + + return $html; } private function editor() diff --git a/src/Fieldtypes/Bard/LinkMark.php b/src/Fieldtypes/Bard/LinkMark.php index 727ecce2703..8bf5cfb7b81 100644 --- a/src/Fieldtypes/Bard/LinkMark.php +++ b/src/Fieldtypes/Bard/LinkMark.php @@ -64,11 +64,13 @@ protected function convertHref($href) return ''; } - if (! $this->isApi() && $item instanceof Entry) { + $selectAcrossSites = Augmentor::$currentBardConfig['select_across_sites'] ?? false; + + if (! $selectAcrossSites && ! $this->isApi() && $item instanceof Entry) { return ($item->in(Site::current()->handle()) ?? $item)->url(); } - return $item->url(); + return $selectAcrossSites ? $item->absoluteUrl() : $item->url(); } private function isApi() diff --git a/tests/Fieldtypes/BardTest.php b/tests/Fieldtypes/BardTest.php index c81004c0672..cf43e330fe9 100644 --- a/tests/Fieldtypes/BardTest.php +++ b/tests/Fieldtypes/BardTest.php @@ -1338,6 +1338,54 @@ public function it_calls_hooks() $this->assertArrayHasKey('custom_field', $bard->extraValidationAttributes($data)); } + #[Test] + public function it_localizes_when_select_across_sites_setting_is_disabled() + { + $this->setSites([ + 'en' => ['url' => 'http://localhost/', 'locale' => 'en'], + 'fr' => ['url' => 'http://localhost/fr/', 'locale' => 'fr'], + ]); + + Facades\Site::setCurrent('fr'); + + tap(Facades\Collection::make('blog')->routes('blog/{slug}'))->sites(['en', 'fr'])->save(); + + EntryFactory::id('parent')->collection('blog')->slug('theparent')->id(123)->locale('en')->create(); + EntryFactory::id('123-fr')->origin('123')->locale('fr')->collection('blog')->slug('one-fr')->data(['title' => 'Le One', 'test' => ['type' => 'link', 'attrs' => ['href' => 'statamic://entry::123-fr']]])->create(); + + $field = (new Bard)->setField(new Field('test', array_merge(['type' => 'bard'], ['select_across_sites' => false]))); + + $augmented = $field->augment([ + ['type' => 'text', 'marks' => [['type' => 'link', 'attrs' => ['href' => 'statamic://entry::123-fr']]], 'text' => 'The One'], + ]); + + $this->assertEquals('The One', $augmented); + } + + #[Test] + public function it_doesnt_localize_when_select_across_sites_setting_is_enabled() + { + $this->setSites([ + 'en' => ['url' => 'http://localhost/', 'locale' => 'en'], + 'fr' => ['url' => 'http://localhost/fr/', 'locale' => 'fr'], + ]); + + Facades\Site::setCurrent('en'); + + tap(Facades\Collection::make('blog')->routes('blog/{slug}'))->sites(['en', 'fr'])->save(); + + EntryFactory::id('parent')->collection('blog')->slug('theparent')->id(123)->locale('en')->create(); + EntryFactory::id('123-fr')->origin('123')->locale('fr')->collection('blog')->slug('one-fr')->data(['title' => 'Le One', 'test' => ['type' => 'link', 'attrs' => ['href' => 'statamic://entry::123-fr']]])->create(); + + $field = (new Bard)->setField(new Field('test', array_merge(['type' => 'bard'], ['select_across_sites' => true]))); + + $augmented = $field->augment([ + ['type' => 'text', 'marks' => [['type' => 'link', 'attrs' => ['href' => 'statamic://entry::123-fr']]], 'text' => 'The One'], + ]); + + $this->assertEquals('The One', $augmented); + } + private function bard($config = []) { return (new Bard)->setField(new Field('test', array_merge(['type' => 'bard', 'sets' => ['one' => []]], $config)));