44
55use Illuminate \Http \Request ;
66use Laravel \Nova \Fields \Boolean ;
7+ use Laravel \Nova \Fields \Code ;
78use Laravel \Nova \Fields \ID ;
89use Laravel \Nova \Fields \Text ;
910use Laravel \Nova \Fields \Trix ;
@@ -38,86 +39,8 @@ public static function indexQuery(NovaRequest $request, $query)
3839 return $ query ->where ('id ' , 1 );
3940 }
4041
41- private static function localesSorted (): array
42- {
43- $ locales = config ('app.locales ' , ['en ' ]);
44- if (is_string ($ locales )) {
45- $ locales = array_map ('trim ' , explode (', ' , $ locales ));
46- }
47- $ locales = array_values (array_filter ($ locales ));
48- if (empty ($ locales )) {
49- $ locales = ['en ' ];
50- }
51- sort ($ locales );
52-
53- return $ locales ;
54- }
55-
5642 public function fields (Request $ request ): array
5743 {
58- $ translationKeys = [
59- 'hero_title ' => 'Hero title ' ,
60- 'hero_subtitle ' => 'Hero subtitle ' ,
61- 'intro_title ' => 'Intro title ' ,
62- 'intro_paragraph_1 ' => 'Intro paragraph 1 ' ,
63- 'intro_paragraph_2 ' => 'Intro paragraph 2 ' ,
64- 'details_title ' => 'Details title ' ,
65- 'details_paragraph_1 ' => 'Details paragraph 1 ' ,
66- 'details_paragraph_2 ' => 'Details paragraph 2 ' ,
67- 'details_paragraph_3 ' => 'Details paragraph 3 ' ,
68- 'details_paragraph_4 ' => 'Details paragraph 4 ' ,
69- 'recap_button_text ' => 'Recap button text ' ,
70- 'toolkit_button_text ' => 'Toolkit button text ' ,
71- ];
72-
73- $ longTextKeys = [
74- 'hero_subtitle ' ,
75- 'intro_paragraph_1 ' ,
76- 'intro_paragraph_2 ' ,
77- 'details_paragraph_1 ' ,
78- 'details_paragraph_2 ' ,
79- 'details_paragraph_3 ' ,
80- 'details_paragraph_4 ' ,
81- ];
82-
83- $ translationFields = [];
84- foreach (self ::localesSorted () as $ locale ) {
85- if ($ locale === 'en ' ) {
86- continue ;
87- }
88-
89- foreach ($ translationKeys as $ key => $ label ) {
90- $ fieldName = 'locale_ ' . $ locale . '_ ' . $ key ;
91- if (in_array ($ key , $ longTextKeys , true )) {
92- $ translationFields [] = Trix::make ($ label . ' ( ' . strtoupper ($ locale ) . ') ' , $ fieldName )
93- ->nullable ()
94- ->resolveUsing (function () use ($ locale , $ key ) {
95- $ overrides = $ this ->resource ->locale_overrides ?? [];
96-
97- return $ overrides [$ locale ][$ key ] ?? '' ;
98- })
99- ->fillUsing (function ($ request , $ model , $ attribute , $ requestAttribute ) use ($ locale , $ key ) {
100- $ overrides = $ model ->locale_overrides ?? [];
101- $ overrides [$ locale ][$ key ] = $ request ->get ($ requestAttribute ) ?: null ;
102- $ model ->locale_overrides = $ overrides ;
103- });
104- } else {
105- $ translationFields [] = Text::make ($ label . ' ( ' . strtoupper ($ locale ) . ') ' , $ fieldName )
106- ->nullable ()
107- ->resolveUsing (function () use ($ locale , $ key ) {
108- $ overrides = $ this ->resource ->locale_overrides ?? [];
109-
110- return $ overrides [$ locale ][$ key ] ?? '' ;
111- })
112- ->fillUsing (function ($ request , $ model , $ attribute , $ requestAttribute ) use ($ locale , $ key ) {
113- $ overrides = $ model ->locale_overrides ?? [];
114- $ overrides [$ locale ][$ key ] = $ request ->get ($ requestAttribute ) ?: null ;
115- $ model ->locale_overrides = $ overrides ;
116- });
117- }
118- }
119- }
120-
12144 $ fields = [
12245 ID ::make ()->onlyOnForms (),
12346 Boolean::make ('Use dynamic content ' , 'dynamic_content ' )
@@ -141,17 +64,20 @@ public function fields(Request $request): array
14164 Trix::make ('Details paragraph 3 ' , 'details_paragraph_3 ' )->nullable (),
14265 Trix::make ('Details paragraph 4 ' , 'details_paragraph_4 ' )->nullable (),
14366 Text::make ('Video URL (embed) ' , 'video_url ' )->nullable (),
67+ Text::make ('Extra button text (optional) ' , 'extra_button_text ' )->nullable (),
68+ Text::make ('Extra button link (optional) ' , 'extra_button_link ' )->nullable (),
14469 Text::make ('Recap button text ' , 'recap_button_text ' )->nullable (),
14570 Text::make ('Recap button link ' , 'recap_button_link ' )->nullable (),
14671 Text::make ('Toolkit button text ' , 'toolkit_button_text ' )->nullable (),
14772 Text::make ('Toolkit button link ' , 'toolkit_button_link ' )->nullable (),
14873 ])->collapsable ()->collapsedByDefault (),
74+ Panel::make ('Translations (JSON) ' , [
75+ Code::make ('Locale overrides ' , 'locale_overrides ' )
76+ ->json ()
77+ ->help ('Optional per-locale overrides. Example: {"fr":{"hero_title":"Hackathons"}} ' ),
78+ ])->collapsable ()->collapsedByDefault (),
14979 ];
15080
151- if (!empty ($ translationFields )) {
152- $ fields [] = new Panel ('Translations ' , $ translationFields );
153- }
154-
15581 return $ fields ;
15682 }
15783}
0 commit comments