generated from filamentphp/plugin-skeleton
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGlobalBlockConfigResource.php
More file actions
111 lines (94 loc) · 3.63 KB
/
GlobalBlockConfigResource.php
File metadata and controls
111 lines (94 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
namespace Redberry\PageBuilderPlugin\Resources;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Redberry\PageBuilderPlugin\Models\GlobalBlockConfig;
use Redberry\PageBuilderPlugin\Resources\GlobalBlockConfigResource\Pages;
class GlobalBlockConfigResource extends Resource
{
protected static ?string $model = GlobalBlockConfig::class;
protected static ?string $navigationIcon = 'heroicon-o-cube';
protected static ?string $navigationGroup = 'Content Management';
protected static ?string $navigationLabel = 'Global Blocks';
protected static ?string $modelLabel = 'Global Block';
protected static ?string $pluralModelLabel = 'Global Blocks';
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('name')
->label('Block Name')
->required()
->disabled(),
Forms\Components\Section::make('Block Configuration')
->schema(function (?GlobalBlockConfig $record) {
if (! $record || ! class_exists($record->class_name)) {
return [];
}
try {
$blockClass = $record->class_name;
if (method_exists($blockClass, 'getBaseBlockSchema')) {
$schema = $blockClass::getBaseBlockSchema();
} else {
$schema = $blockClass::getBlockSchema();
}
foreach ($schema as $field) {
if (method_exists($field, 'getName')) {
$fieldName = $field->getName();
$configValue = $record->getConfigValue($fieldName);
if ($configValue !== null) {
$field->default($configValue);
}
}
}
return $schema;
} catch (\Exception $e) {
return [
Forms\Components\Placeholder::make('error')
->label('Error')
->content('Unable to load block schema: ' . $e->getMessage()),
];
}
})
->columnSpan(2),
]);
}
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('name')
->label('Block Name')
->searchable()
->sortable(),
])
->actions([
Tables\Actions\EditAction::make()
->label('Configure'),
])
->bulkActions([
//
]);
}
public static function getEloquentQuery(): Builder
{
return parent::getEloquentQuery()->orderBy('name');
}
public static function getRelations(): array
{
return [
//
];
}
public static function getPages(): array
{
return [
'index' => Pages\ListGlobalBlocks::route('/'),
'edit' => Pages\EditGlobalBlock::route('/{record}/edit'),
];
}
}