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] Support fieldsets in subdirectories #9341

Open
wants to merge 12 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
4 changes: 3 additions & 1 deletion resources/js/components/blueprints/LinkFields.vue
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,9 @@ export default {
methods: {

linkField() {
const [fieldsetHandle, fieldHandle] = this.reference.split('.');
const lastDot = this.reference.lastIndexOf('.');
const fieldsetHandle = this.reference.substring(0, lastDot);
const fieldHandle = this.reference.substring(lastDot + 1);

const field = this.fieldsets
.find(fieldset => fieldset.handle === fieldsetHandle).fields
Expand Down
8 changes: 8 additions & 0 deletions resources/js/components/fieldsets/CreateForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
<div class="text-2xs text-gray-600 mt-2 flex items-center">
{{ __('messages.fieldsets_title_instructions') }}
</div>
<div v-if="errors.title">
<small class="help-block text-red-500 mt-2 mb-0" v-for="(error, i) in errors.title" :key="i" v-text="error" />
</div>
</div>
<div class="mb-4">
<label class="font-bold text-base mb-1" for="name">{{ __('Handle') }}</label>
Expand All @@ -22,6 +25,9 @@
<div class="text-2xs text-gray-600 mt-2 flex items-center">
{{ __('messages.fieldsets_handle_instructions') }}
</div>
<div v-if="errors.handle">
<small class="help-block text-red-500 mt-2 mb-0" v-for="(error, i) in errors.handle" :key="i" v-text="error" />
</div>
</div>
</div>

Expand All @@ -48,6 +54,7 @@ export default {
title: null,
handle: null,
slug: this.$slug.async().separatedBy('_'),
errors: [],
}
},

Expand All @@ -68,6 +75,7 @@ export default {
this.$axios.post(this.route, {title: this.title, handle: this.handle}).then(response => {
window.location = response.data.redirect;
}).catch(error => {
this.errors = error.response.data.errors;
this.$toast.error(error.response.data.message);
});
}
Expand Down
3 changes: 2 additions & 1 deletion src/Fields/FieldRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public function find(string $field): ?Field
return null;
}

[$fieldset, $handle] = explode('.', $field);
$fieldset = Str::beforeLast($field, '.');
$handle = Str::afterLast($field, '.');

if (! $fieldset = $this->fieldsets->find($fieldset)) {
return null;
Expand Down
2 changes: 1 addition & 1 deletion src/Fields/FieldsetRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public function delete(Fieldset $fieldset)
throw new \Exception('Namespaced fieldsets cannot be deleted');
}

File::delete("{$this->directory}/{$fieldset->handle()}.yaml");
File::delete($fieldset->path());
}

public function reset(Fieldset $fieldset)
Expand Down
16 changes: 12 additions & 4 deletions src/Http/Controllers/CP/Fields/FieldsetController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Statamic\Fields\Fieldset;
use Statamic\Fields\FieldTransformer;
use Statamic\Http\Controllers\CP\CpController;
use Statamic\Rules\Handle;
use Statamic\Support\Arr;
use Statamic\Support\Str;

Expand Down Expand Up @@ -40,7 +39,8 @@ public function index(Request $request)
'title' => $fieldset->title(),
],
];
});
})
->sortBy(fn ($value, $key) => $key === __('My Fieldsets') ? '0' : $key);

if ($request->wantsJson()) {
return $fieldsets;
Expand Down Expand Up @@ -124,7 +124,7 @@ public function store(Request $request)
{
$request->validate([
'title' => 'required',
'handle' => ['required', new Handle],
'handle' => ['required', 'regex:/^[a-zA-Z0-9._-]+$/'],
]);

if (Facades\Fieldset::find($request->handle)) {
Expand Down Expand Up @@ -173,6 +173,14 @@ public function reset($fieldset)

private function groupKey(Fieldset $fieldset): string
{
return $fieldset->isNamespaced() ? Str::of($fieldset->namespace())->replace('_', ' ')->title() : __('My Fieldsets');
if ($fieldset->isNamespaced()) {
return Str::of($fieldset->namespace())->replace('_', ' ')->title();
}

if (str_contains($fieldset->handle(), '.')) {
return Str::of($fieldset->handle())->beforeLast('.')->title();
}

return __('My Fieldsets');
}
}
22 changes: 22 additions & 0 deletions tests/Feature/Fieldsets/StoreFieldsetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,28 @@ public function handle_must_be_alpha_dash()
->assertSessionHasErrors('handle');
}

#[Test]
public function fieldset_gets_created_in_subdirectory()
{
$user = tap(Facades\User::make()->makeSuper())->save();
$this->assertCount(0, Facades\Fieldset::all());

$this
->actingAs($user)
->submit(['handle' => 'components.test'])
->assertOk()
->assertJson(['redirect' => cp_route('fieldsets.edit', 'components.test')])
->assertSessionHas('success');

$this->assertCount(1, Facades\Fieldset::all());
$fieldset = Facades\Fieldset::find('components.test');
$this->assertEquals([
'title' => 'Test',
'fields' => [],
], $fieldset->contents());
$this->assertEquals('components.test', $fieldset->handle());
}

private function submit($params = [])
{
return $this->post(cp_route('fieldsets.store'), $this->validParams($params));
Expand Down
Loading