-
-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathEditArticle.php
More file actions
69 lines (62 loc) · 2.41 KB
/
EditArticle.php
File metadata and controls
69 lines (62 loc) · 2.41 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
<?php
namespace App\Filament\Resources\ArticleResource\Pages;
use App\Filament\Resources\ArticleResource;
use App\Services\OgImageService;
use Filament\Actions;
use Filament\Forms\Components\DateTimePicker;
use Filament\Forms\Components\Radio;
use Filament\Resources\Pages\EditRecord;
use Illuminate\Support\Facades\Date;
class EditArticle extends EditRecord
{
protected static string $resource = ArticleResource::class;
protected function afterSave(): void
{
$service = resolve(OgImageService::class);
// Always regenerate the OG image on update to ensure it's current
$ogImageUrl = $service->generate($this->record);
$this->record->update([
'og_image' => $ogImageUrl,
]);
}
protected function getHeaderActions(): array
{
return [
Actions\Action::make('preview')
->label('Preview')
->icon('heroicon-o-eye')
->url(fn () => route('article', $this->record))
->openUrlInNewTab(),
Actions\Action::make('publish')
->label('Publish')
->icon('heroicon-o-newspaper')
->visible(fn () => ! $this->record->isPublished())
->form([
Radio::make('publish_type')
->label('Publish Options')
->options([
'now' => 'Publish Now',
'schedule' => 'Schedule for Later',
])
->default('now')
->live()
->required(),
DateTimePicker::make('published_at')
->label('Published At')
->displayFormat('M j, Y H:i')
->seconds(false)
->afterOrEqual('now')
->visible(fn ($get) => $get('publish_type') === 'schedule')
->required(fn ($get) => $get('publish_type') === 'schedule'),
])
->action(function (array $data): void {
if ($data['publish_type'] === 'now') {
$this->record->publish();
} else {
$this->record->publish(Date::parse($data['published_at']));
}
}),
Actions\DeleteAction::make(),
];
}
}