Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/Fieldtypes/Bard/Augmentor.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,6 +13,7 @@

class Augmentor
{
public static $currentBardConfig = [];
protected $fieldtype;
protected $sets = [];
protected $includeDisabledSets = false;
Expand DownExpand Up@@ -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()
Expand Down
6 changes: 4 additions & 2 deletions src/Fieldtypes/Bard/LinkMark.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -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()
Expand Down
48 changes: 48 additions & 0 deletions tests/Fieldtypes/BardTest.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -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('<a href="/fr/blog/one-fr">The One</a>', $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('<a href="http://localhost/fr/blog/one-fr">The One</a>', $augmented);
}

private function bard($config = [])
{
return (new Bard)->setField(new Field('test', array_merge(['type' => 'bard', 'sets' => ['one' => []]], $config)));
Expand Down