22
33namespace Vormkracht10 \MediaField ;
44
5- class MediaField {}
5+ use Filament \Forms ;
6+ use Vormkracht10 \Fields \Fields \Base ;
7+ use Vormkracht10 \Fields \Models \Field ;
8+ use Illuminate \Database \Eloquent \Model ;
9+ use Vormkracht10 \Backstage \Models \Media as MediaModel ;
10+ use Vormkracht10 \MediaPicker \MediaPicker ;
11+ use Vormkracht10 \Fields \Contracts \FieldContract ;
12+ use Vormkracht10 \MediaPicker \Components \MediaPicker as Input ;
13+
14+ class Media extends Base implements FieldContract
15+ {
16+ public static function getDefaultConfig (): array
17+ {
18+ return [
19+ ...parent ::getDefaultConfig (),
20+ 'acceptedFileTypes ' => ['image/* ' , 'video/* ' , 'audio/* ' , 'application/pdf ' ],
21+ 'multiple ' => false ,
22+ ];
23+ }
24+
25+ public static function make (string $ name , Field $ field ): Input
26+ {
27+ $ input = self ::applyDefaultSettings (Input::make ($ name ), $ field );
28+
29+ if (! empty ($ field ->config ['acceptedFileTypes ' ]) && ! is_array ($ field ->config ['acceptedFileTypes ' ])) {
30+ $ field ->config ['acceptedFileTypes ' ] = [$ field ->config ['acceptedFileTypes ' ]];
31+ }
32+
33+ $ input = $ input ->label ($ field ->name ?? self ::getDefaultConfig ()['label ' ] ?? null )
34+ ->acceptedFileTypes ($ field ->config ['acceptedFileTypes ' ] ?? self ::getDefaultConfig ()['acceptedFileTypes ' ])
35+ ->multiple ($ field ->config ['multiple ' ] ?? self ::getDefaultConfig ()['multiple ' ]);
36+
37+ return $ input ;
38+ }
39+
40+ public function getForm (): array
41+ {
42+ return [
43+ Forms \Components \Tabs::make ()
44+ ->schema ([
45+ Forms \Components \Tabs \Tab::make ('General ' )
46+ ->label (__ ('General ' ))
47+ ->schema ([
48+ ...parent ::getForm (),
49+ ]),
50+ Forms \Components \Tabs \Tab::make ('Field specific ' )
51+ ->label (__ ('Field specific ' ))
52+ ->schema ([
53+ Forms \Components \Grid::make (2 )->schema ([
54+ Forms \Components \Select::make ('config.acceptedFileTypes ' )
55+ ->label (__ ('Accepted file types ' ))
56+ ->options ([
57+ 'image/* ' => 'Images ' ,
58+ 'video/* ' => 'Videos ' ,
59+ 'audio/* ' => 'Audio ' ,
60+ 'application/pdf ' => 'PDF ' ,
61+ ])
62+ ->multiple (),
63+ Forms \Components \Toggle::make ('config.multiple ' )
64+ ->label (__ ('Multiple ' )),
65+ ]),
66+ ]),
67+ ])->columnSpanFull (),
68+ ];
69+ }
70+
71+ public static function mutateFormDataCallback (Model $ record , Field $ field , array $ data ): array
72+ {
73+ if (! isset ($ record ->values [$ field ->slug ])) {
74+ return $ data ;
75+ }
76+
77+ $ media = MediaModel::whereIn ('ulid ' , $ record ->values [$ field ->slug ])
78+ ->get ()
79+ ->map (function ($ media ) {
80+ return 'media/ ' . $ media ->filename ;
81+ })->toArray ();
82+
83+ $ data ['value ' ][$ field ->slug ] = $ media ;
84+
85+ return $ data ;
86+ }
87+
88+ public static function mutateBeforeSaveCallback (Model $ record , Field $ field , array $ data ): array
89+ {
90+ if ($ field ->field_type !== 'media ' ) {
91+ return $ data ;
92+ }
93+
94+ if (! isset ($ data ['value ' ][$ field ->slug ])) {
95+ return $ data ;
96+ }
97+
98+ $ media = MediaPicker::create ($ data ['value ' ][$ field ->slug ]);
99+
100+ $ data ['value ' ][$ field ->slug ] = collect ($ media )->map (function ($ media ) {
101+ return $ media ->ulid ;
102+ })->toArray ();
103+
104+ return $ data ;
105+ }
106+ }
0 commit comments