Skip to content

Commit 3010b1e

Browse files
committed
Custom Fields Resource Configuration
1 parent b700727 commit 3010b1e

File tree

5 files changed

+198
-41
lines changed

5 files changed

+198
-41
lines changed

README.md

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,26 @@ default configuration:
296296

297297
```php
298298
return [
299+
/*
300+
|--------------------------------------------------------------------------
301+
| Custom Fields Resource Configuration
302+
|--------------------------------------------------------------------------
303+
|
304+
| This section controls the Custom Fields resource.
305+
| This allows you to customize the behavior of the resource.
306+
|
307+
*/
308+
'custom_fields_resource' => [
309+
'should_register_navigation' => true,
310+
'slug' => 'custom-fields',
311+
'navigation_sort' => -1,
312+
'navigation_badge' => false,
313+
'navigation_group' => true,
314+
'is_globally_searchable' => false,
315+
'is_scoped_to_tenant' => true,
316+
'cluster' => null,
317+
],
318+
299319
/*
300320
|--------------------------------------------------------------------------
301321
| Entity Resources Configuration
@@ -314,6 +334,24 @@ return [
314334
//
315335
],
316336

337+
/*
338+
|--------------------------------------------------------------------------
339+
| Entity Resources Customization
340+
|--------------------------------------------------------------------------
341+
|
342+
| This section allows you to customize the behavior of entity resources,
343+
| such as enabling table column toggling and setting default visibility.
344+
|
345+
*/
346+
'resource' => [
347+
'table' => [
348+
'columns_toggleable' => [
349+
'enabled' => true,
350+
'hidden_by_default' => true,
351+
],
352+
],
353+
],
354+
317355
/*
318356
|--------------------------------------------------------------------------
319357
| Lookup Resources Configuration
@@ -334,33 +372,30 @@ return [
334372

335373
/*
336374
|--------------------------------------------------------------------------
337-
| Resource Table Configuration
375+
| Tenant Awareness Configuration
338376
|--------------------------------------------------------------------------
339377
|
340-
| This section allows you to customize the behavior of resource tables,
341-
| such as enabling column toggling and setting default visibility.
378+
| When enabled, this feature implements multi-tenancy using the specified
379+
| tenant foreign key. Enable this before running migrations to automatically
380+
| register the tenant foreign key.
342381
|
343382
*/
344-
'resource' => [
345-
'table' => [
346-
'columns_toggleable' => [
347-
'enabled' => true,
348-
'hidden_by_default' => true,
349-
],
350-
],
351-
],
383+
'tenant_aware' => true,
384+
352385

353386
/*
354387
|--------------------------------------------------------------------------
355-
| Tenant Awareness Configuration
388+
| Database Migrations Paths
356389
|--------------------------------------------------------------------------
357390
|
358-
| When enabled, this feature implements multi-tenancy using the specified
359-
| tenant foreign key. Enable this before running migrations to automatically
360-
| register the tenant foreign key.
391+
| In these directories custom fields migrations will be stored and ran when migrating. A custom fields
392+
| migration created via the make:custom-fields-migration command will be stored in the first path or
393+
| a custom defined path when running the command.
361394
|
362395
*/
363-
'tenant_aware' => false,
396+
'migrations_paths' => [
397+
database_path('custom-fields'),
398+
],
364399

365400
/*
366401
|--------------------------------------------------------------------------
@@ -392,6 +427,7 @@ return [
392427
];
393428
```
394429

430+
395431
## Preset Custom Fields
396432

397433
Preset Custom Fields allow developers to programmatically define, deploy, and manage custom fields for Filament

config/custom-fields.php

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,26 @@
11
<?php
22

33
return [
4+
/*
5+
|--------------------------------------------------------------------------
6+
| Custom Fields Resource Configuration
7+
|--------------------------------------------------------------------------
8+
|
9+
| This section controls the Custom Fields resource.
10+
| This allows you to customize the behavior of the resource.
11+
|
12+
*/
13+
'custom_fields_resource' => [
14+
'should_register_navigation' => true,
15+
'slug' => 'custom-fields',
16+
'navigation_sort' => -1,
17+
'navigation_badge' => false,
18+
'navigation_group' => true,
19+
'is_globally_searchable' => false,
20+
'is_scoped_to_tenant' => true,
21+
'cluster' => null,
22+
],
23+
424
/*
525
|--------------------------------------------------------------------------
626
| Entity Resources Configuration
@@ -19,6 +39,24 @@
1939
//
2040
],
2141

42+
/*
43+
|--------------------------------------------------------------------------
44+
| Entity Resources Customization
45+
|--------------------------------------------------------------------------
46+
|
47+
| This section allows you to customize the behavior of entity resources,
48+
| such as enabling table column toggling and setting default visibility.
49+
|
50+
*/
51+
'resource' => [
52+
'table' => [
53+
'columns_toggleable' => [
54+
'enabled' => true,
55+
'hidden_by_default' => true,
56+
],
57+
],
58+
],
59+
2260
/*
2361
|--------------------------------------------------------------------------
2462
| Lookup Resources Configuration
@@ -37,24 +75,6 @@
3775
//
3876
],
3977

40-
/*
41-
|--------------------------------------------------------------------------
42-
| Resource Table Configuration
43-
|--------------------------------------------------------------------------
44-
|
45-
| This section allows you to customize the behavior of resource tables,
46-
| such as enabling column toggling and setting default visibility.
47-
|
48-
*/
49-
'resource' => [
50-
'table' => [
51-
'columns_toggleable' => [
52-
'enabled' => true,
53-
'hidden_by_default' => true,
54-
],
55-
],
56-
],
57-
5878
/*
5979
|--------------------------------------------------------------------------
6080
| Tenant Awareness Configuration
@@ -65,7 +85,7 @@
6585
| register the tenant foreign key.
6686
|
6787
*/
68-
'tenant_aware' => false,
88+
'tenant_aware' => true,
6989

7090

7191
/*
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
<?php
22

33
return [
4+
'nav' => [
5+
'label' => 'Custom Fields',
6+
'group' => 'Custom Fields',
7+
'icon' => 'heroicon-o-cube'
8+
]
49
];

src/Filament/Resources/CustomFieldResource.php

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Relaticle\CustomFields\Filament\Resources;
66

7+
use Filament\Facades\Filament;
78
use Filament\Forms;
89
use Filament\Pages\SubNavigationPosition;
910
use Filament\Resources\Resource;
@@ -22,19 +23,14 @@
2223
use Relaticle\CustomFields\Models\Scopes\ActivableScope;
2324
use Relaticle\CustomFields\Services\EntityTypeOptionsService;
2425
use Relaticle\CustomFields\Services\LookupTypeOptionsService;
26+
use Relaticle\CustomFields\Support\Utils;
2527

2628
final class CustomFieldResource extends Resource
2729
{
2830
protected static ?string $model = CustomField::class;
2931

30-
protected static ?string $label = 'Custom Field';
31-
32-
protected static ?string $slug = 'custom-fields';
33-
3432
protected static ?string $tenantOwnershipRelationshipName = 'team';
3533

36-
protected static bool $shouldRegisterNavigation = false;
37-
3834
protected static SubNavigationPosition $subNavigationPosition = SubNavigationPosition::End;
3935

4036
public static function form(Forms\Form $form): Forms\Form
@@ -194,4 +190,58 @@ public static function getPages(): array
194190
'index' => Pages\ManageCustomFields::route('/'),
195191
];
196192
}
193+
194+
public static function getCluster(): ?string
195+
{
196+
return Utils::getResourceCluster() ?? static::$cluster;
197+
}
198+
199+
public static function shouldRegisterNavigation(): bool
200+
{
201+
return Utils::isResourceNavigationRegistered();
202+
}
203+
204+
public static function getNavigationGroup(): ?string
205+
{
206+
return Utils::isResourceNavigationGroupEnabled()
207+
? __('custom-fields::custom-fields.nav.group')
208+
: '';
209+
}
210+
211+
public static function getNavigationLabel(): string
212+
{
213+
return __('custom-fields::custom-fields.nav.label');
214+
}
215+
216+
public static function getNavigationIcon(): string
217+
{
218+
return __('custom-fields::custom-fields.nav.icon');
219+
}
220+
221+
public static function getNavigationSort(): ?int
222+
{
223+
return Utils::getResourceNavigationSort();
224+
}
225+
226+
public static function getSlug(): string
227+
{
228+
return Utils::getResourceSlug();
229+
}
230+
231+
public static function getNavigationBadge(): ?string
232+
{
233+
return Utils::isResourceNavigationBadgeEnabled()
234+
? strval(static::getEloquentQuery()->count())
235+
: null;
236+
}
237+
238+
public static function isScopedToTenant(): bool
239+
{
240+
return Utils::isScopedToTenant();
241+
}
242+
243+
public static function canGloballySearch(): bool
244+
{
245+
return Utils::isResourceGloballySearchable() && count(static::getGloballySearchableAttributes()) && static::canViewAny();
246+
}
197247
}

src/Support/Utils.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Relaticle\CustomFields\Support;
4+
5+
class Utils
6+
{
7+
public static function getResourceCluster(): ?string
8+
{
9+
return config('filament-shield.shield_resource.cluster', null);
10+
}
11+
12+
public static function getResourceSlug(): string
13+
{
14+
return (string) config('custom-fields.custom_fields_resource.slug');
15+
}
16+
17+
public static function isResourceNavigationRegistered(): bool
18+
{
19+
return config('custom-fields.custom_fields_resource.should_register_navigation', true);
20+
}
21+
22+
public static function getResourceNavigationSort(): ?int
23+
{
24+
return config('custom-fields.custom_fields_resource.navigation_sort');
25+
}
26+
27+
public static function isResourceNavigationBadgeEnabled(): bool
28+
{
29+
return config('custom-fields.custom_fields_resource.navigation_badge', false);
30+
}
31+
32+
public static function isScopedToTenant(): bool
33+
{
34+
return config('custom-fields.custom_fields_resource.is_scoped_to_tenant', true);
35+
}
36+
37+
public static function isResourceNavigationGroupEnabled(): bool
38+
{
39+
return config('custom-fields.custom_fields_resource.navigation_group', true);
40+
}
41+
42+
public static function isResourceGloballySearchable(): bool
43+
{
44+
return config('custom-fields.custom_fields_resource.is_globally_searchable', false);
45+
}
46+
}

0 commit comments

Comments
 (0)