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

[5.x] Allow selecting entries from different sites within the link fieldtype #10546

Open
wants to merge 2 commits into
base: 5.x
Choose a base branch
from
Open
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
36 changes: 35 additions & 1 deletion src/Fieldtypes/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
],
],
];
Expand All @@ -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()]
);
}

Expand Down Expand Up @@ -108,6 +114,7 @@ private function nestedEntriesFieldtype($value): Fieldtype
'type' => 'entries',
'max_items' => 1,
'create' => false,
'select_across_sites' => $this->canSelectAcrossSites(),
]));

$entryField->setValue($value);
Expand Down Expand Up @@ -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();
}
}
11 changes: 10 additions & 1 deletion src/Fieldtypes/Link/ArrayableLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Statamic\Fieldtypes\Link;

use Illuminate\Support\Arr;
use Statamic\Fields\ArrayableString;

class ArrayableLink extends ArrayableString
Expand All @@ -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();
}
}
Loading