diff --git a/src/Fieldtypes/Link.php b/src/Fieldtypes/Link.php index 61a9371e56..f8d513f80c 100644 --- a/src/Fieldtypes/Link.php +++ b/src/Fieldtypes/Link.php @@ -37,6 +37,11 @@ protected function configFieldItems(): array 'mode' => 'select', 'max_items' => 1, ], + 'select_across_sites' => [ + 'display' => __('Select Across Sites'), + 'instructions' => __('statamic::fieldtypes.entries.config.select_across_sites'), + 'type' => 'toggle', + ], ], ], ]; @@ -47,7 +52,8 @@ public function augment($value) return new ArrayableLink( $value ? ResolveRedirect::item($value, $this->field->parent(), true) - : null + : null, + ['select_across_sites' => $this->canSelectAcrossSites()] ); } @@ -108,6 +114,7 @@ private function nestedEntriesFieldtype($value): Fieldtype 'type' => 'entries', 'max_items' => 1, 'create' => false, + 'select_across_sites' => $this->canSelectAcrossSites(), ])); $entryField->setValue($value); @@ -190,4 +197,31 @@ public function toGqlType() }, ]; } + + protected function getConfiguredCollections() + { + return empty($collections = $this->config('collections')) + ? \Statamic\Facades\Collection::handles()->all() + : $collections; + } + + private function canSelectAcrossSites(): bool + { + return $this->config('select_across_sites', false); + } + + private function availableSites() + { + if (! Site::hasMultiple()) { + return []; + } + + $configuredSites = collect($this->getConfiguredCollections())->flatMap(fn ($collection) => \Statamic\Facades\Collection::find($collection)->sites()); + + return Site::authorized() + ->when(isset($configuredSites), fn ($sites) => $sites->filter(fn ($site) => $configuredSites->contains($site->handle()))) + ->map->handle() + ->values() + ->all(); + } } diff --git a/src/Fieldtypes/Link/ArrayableLink.php b/src/Fieldtypes/Link/ArrayableLink.php index 34658795f4..25e9a1458c 100644 --- a/src/Fieldtypes/Link/ArrayableLink.php +++ b/src/Fieldtypes/Link/ArrayableLink.php @@ -2,6 +2,7 @@ namespace Statamic\Fieldtypes\Link; +use Illuminate\Support\Arr; use Statamic\Fields\ArrayableString; class ArrayableLink extends ArrayableString @@ -26,6 +27,14 @@ public function jsonSerialize() public function url() { - return is_object($this->value) ? $this->value?->url() : $this->value; + if (! is_object($this->value)) { + return $this->value; + } + + if (Arr::get($this->extra(), 'select_across_sites')) { + return $this->value->absoluteUrl(); + } + + return $this->value?->url(); } }