Skip to content

Commit 23ea0e9

Browse files
jaygeorgejasonvarga
authored andcommitted
Show site groups in listing Site columns
Render Group ▸ Site in index views so entry listings match Working In and site selectors when sites are grouped.
1 parent c3838ba commit 23ea0e9

4 files changed

Lines changed: 138 additions & 2 deletions

File tree

resources/js/bootstrap/fieldtypes.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import FieldsFieldtype from '../components/fieldtypes/grid/FieldsFieldtype.vue';
2828
import FilesFieldtype from '../components/fieldtypes/FilesFieldtype.vue';
2929
import FloatFieldtype from '../components/fieldtypes/FloatFieldtype.vue';
3030
import Sites from '../components/globals/Sites.vue';
31+
import SitesIndexFieldtype from '../components/fieldtypes/SitesIndexFieldtype.vue';
3132
import Grid from '../components/fieldtypes/grid/Grid.vue';
3233
import GridIndex from '../components/fieldtypes/grid/GridIndex.vue';
3334
import GroupFieldtype from '../components/fieldtypes/GroupFieldtype.vue';
@@ -102,6 +103,7 @@ export default function registerFieldtypes(app) {
102103
app.component('files-fieldtype', FilesFieldtype);
103104
app.component('float-fieldtype', FloatFieldtype);
104105
app.component('global_set_sites-fieldtype', Sites);
106+
app.component('sites-fieldtype-index', SitesIndexFieldtype);
105107
app.component('grid-fieldtype', Grid);
106108
app.component('grid-fieldtype-index', GridIndex);
107109
app.component('group-fieldtype', GroupFieldtype);
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<template>
2+
<div class="flex flex-wrap gap-x-3 gap-y-1">
3+
<ItemLabel
4+
v-for="(item, index) in items"
5+
:key="index"
6+
:item="item"
7+
:group-label="groupLabel(item)"
8+
/>
9+
</div>
10+
</template>
11+
12+
<script>
13+
import IndexFieldtype from './IndexFieldtype.vue';
14+
import ItemLabel from '../inputs/relationship/ItemLabel.vue';
15+
import {
16+
hasNamedSiteGroups,
17+
selectedSiteGroupLabel,
18+
} from '@/util/site-groups.js';
19+
20+
export default {
21+
mixins: [IndexFieldtype],
22+
23+
components: {
24+
ItemLabel,
25+
},
26+
27+
computed: {
28+
items() {
29+
if (!this.value) {
30+
return [];
31+
}
32+
33+
return Array.isArray(this.value) ? this.value : [this.value];
34+
},
35+
36+
namedGroupsExist() {
37+
return hasNamedSiteGroups(Statamic.$config.get('sites') || []);
38+
},
39+
},
40+
41+
methods: {
42+
groupLabel(item) {
43+
return selectedSiteGroupLabel(item, this.namedGroupsExist);
44+
},
45+
},
46+
};
47+
</script>

src/Fieldtypes/Sites.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
class Sites extends Relationship
99
{
10-
protected $indexComponent = 'text';
10+
protected $indexComponent = 'sites';
1111

1212
protected function authorizeItemData($id): bool
1313
{
@@ -64,6 +64,14 @@ public function preProcessIndex($data)
6464
$items = collect([$items]);
6565
}
6666

67-
return $items->map->name()->join(', ');
67+
return $items
68+
->filter()
69+
->map(fn ($site) => [
70+
'title' => $site->name(),
71+
'group' => $site->group(),
72+
'group_handle' => $site->groupHandle(),
73+
])
74+
->values()
75+
->all();
6876
}
6977
}

tests/Fieldtypes/SitesTest.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
namespace Tests\Fieldtypes;
4+
5+
use PHPUnit\Framework\Attributes\Test;
6+
use Statamic\Fields\Field;
7+
use Statamic\Fieldtypes\Sites;
8+
use Tests\TestCase;
9+
10+
class SitesTest extends TestCase
11+
{
12+
#[Test]
13+
public function it_preprocesses_index_values_with_site_groups()
14+
{
15+
$this->setSites([
16+
'en' => [
17+
'name' => 'English',
18+
'locale' => 'en_US',
19+
'url' => '/',
20+
'group' => 'UK',
21+
'group_handle' => 'uk',
22+
],
23+
'fr' => [
24+
'name' => 'French',
25+
'locale' => 'fr_FR',
26+
'url' => '/fr/',
27+
'group' => 'EU',
28+
'group_handle' => 'eu',
29+
],
30+
]);
31+
32+
$this->assertEquals([
33+
[
34+
'title' => 'English',
35+
'group' => 'UK',
36+
'group_handle' => 'uk',
37+
],
38+
], $this->fieldtype(['max_items' => 1])->preProcessIndex('en'));
39+
40+
$this->assertEquals([
41+
[
42+
'title' => 'English',
43+
'group' => 'UK',
44+
'group_handle' => 'uk',
45+
],
46+
[
47+
'title' => 'French',
48+
'group' => 'EU',
49+
'group_handle' => 'eu',
50+
],
51+
], $this->fieldtype()->preProcessIndex(['en', 'fr']));
52+
}
53+
54+
#[Test]
55+
public function it_preprocesses_index_values_without_site_groups()
56+
{
57+
$this->setSites([
58+
'en' => ['name' => 'English', 'locale' => 'en_US', 'url' => '/'],
59+
'fr' => ['name' => 'French', 'locale' => 'fr_FR', 'url' => '/fr/'],
60+
]);
61+
62+
$this->assertEquals([
63+
[
64+
'title' => 'English',
65+
'group' => null,
66+
'group_handle' => null,
67+
],
68+
], $this->fieldtype(['max_items' => 1])->preProcessIndex('en'));
69+
}
70+
71+
private function fieldtype($config = [])
72+
{
73+
$field = new Field('test', array_merge([
74+
'type' => 'sites',
75+
], $config));
76+
77+
return (new Sites)->setField($field);
78+
}
79+
}

0 commit comments

Comments
 (0)