Skip to content

Commit 8ea79cb

Browse files
authored
Merge pull request #3346 from codeeu/dev
Dev
2 parents 3bddf28 + 018140d commit 8ea79cb

2 files changed

Lines changed: 119 additions & 70 deletions

File tree

‎app/GirlsInDigitalPage.php‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,20 @@ class GirlsInDigitalPage extends Model
6363
'locale_overrides' => 'array',
6464
];
6565

66+
/**
67+
* Prevent relationship / virtual attributes from being persisted (e.g. from Nova form).
68+
*/
69+
public function setAttribute($key, $value)
70+
{
71+
if (in_array($key, ['faqItems'], true)) {
72+
return $this;
73+
}
74+
if (str_starts_with($key, 'button_') || str_starts_with($key, 'locale_')) {
75+
return $this;
76+
}
77+
return parent::setAttribute($key, $value);
78+
}
79+
6680
public function buttons()
6781
{
6882
return $this->hasMany(GirlsInDigitalButton::class, 'page_id')->orderBy('position');

‎app/Nova/GirlsInDigitalPage.php‎

Lines changed: 105 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ public static function uriKey(): string
7272
return 'girls-in-digital-page';
7373
}
7474

75+
/**
76+
* Permissive update rules so form submission does not fail on strict validation.
77+
*/
78+
public static function rulesForUpdate(NovaRequest $request, $resource = null): array
79+
{
80+
return [];
81+
}
82+
7583
public static function indexQuery(NovaRequest $request, $query)
7684
{
7785
return $query->where('id', 1);
@@ -156,113 +164,140 @@ public function fields(Request $request): array
156164
}
157165
}
158166

167+
/**
168+
* Build locale fields for "Other languages" (non-English). English is always first in each section.
169+
*/
170+
private function otherLocaleFields(array $locales, array $specs): array
171+
{
172+
$fields = [];
173+
foreach ($locales as $locale) {
174+
if ($locale === 'en') {
175+
continue;
176+
}
177+
foreach ($specs as $spec) {
178+
$fields[] = $this->localeField(
179+
$spec['label'] . ' (' . strtoupper($locale) . ')',
180+
$locale,
181+
$spec['key'],
182+
$spec['textarea'] ?? false,
183+
$spec['rich'] ?? false
184+
);
185+
}
186+
}
187+
return $fields;
188+
}
189+
159190
private function buildFields(Request $request): array
160191
{
161192
$locales = self::localesSorted();
162193

194+
// — Hero: English first, then other languages, then buttons
163195
$heroFields = [
164196
Boolean::make('Use dynamic content for this section', 'hero_dynamic')
165197
->help('When on, hero uses the content below.'),
166-
Trix::make('Hero intro (default)', 'hero_intro')->nullable()->help('Use the toolbar for bold, italic, links, lists.'),
167-
Text::make('Hero video URL (default)', 'hero_video_url')->nullable()->help('YouTube embed URL.'),
168-
Text::make('Hero image (default)', 'hero_image')->nullable(),
198+
\Laravel\Nova\Fields\Heading::make('English'),
199+
Trix::make('Hero intro (English)', 'hero_intro')->nullable()->help('Use the toolbar for bold, italic, links, lists.'),
200+
Text::make('Hero video URL (English)', 'hero_video_url')->nullable()->help('YouTube embed URL.'),
201+
Text::make('Hero image (English)', 'hero_image')->nullable(),
169202
];
170-
foreach ($locales as $locale) {
171-
if ($locale === 'en') {
172-
continue;
173-
}
174-
$heroFields[] = $this->localeField('Hero intro (' . strtoupper($locale) . ')', $locale, 'hero_intro', true, true);
175-
$heroFields[] = $this->localeField('Hero video URL (' . strtoupper($locale) . ')', $locale, 'hero_video_url', false);
176-
}
203+
$heroFields[] = \Laravel\Nova\Fields\Heading::make('Other languages');
204+
$heroFields = array_merge($heroFields, $this->otherLocaleFields($locales, [
205+
['label' => 'Hero intro', 'key' => 'hero_intro', 'textarea' => true, 'rich' => true],
206+
['label' => 'Hero video URL', 'key' => 'hero_video_url'],
207+
]));
177208
foreach (self::buttonKeysBySection()['hero'] as $key => $title) {
178209
$heroFields = array_merge($heroFields, $this->buttonFields($key, $title));
179210
}
180211

212+
// — About: English first, then other languages, then buttons
181213
$aboutFields = [
182214
Boolean::make('Use dynamic content for this section', 'about_dynamic'),
183-
Text::make('About title (default)', 'about_title')->nullable(),
184-
Trix::make('About description 1 (default)', 'about_description_1')->nullable()->help('Use the toolbar for bold, italic, links, lists.'),
185-
Trix::make('About description 2 (default)', 'about_description_2')->nullable()->help('Use the toolbar for bold, italic, links, lists.'),
186-
Text::make('About image (default)', 'about_image')->nullable(),
215+
\Laravel\Nova\Fields\Heading::make('English'),
216+
Text::make('About title (English)', 'about_title')->nullable(),
217+
Trix::make('About description 1 (English)', 'about_description_1')->nullable()->help('Use the toolbar for bold, italic, links, lists.'),
218+
Trix::make('About description 2 (English)', 'about_description_2')->nullable()->help('Use the toolbar for bold, italic, links, lists.'),
219+
Text::make('About image (English)', 'about_image')->nullable(),
187220
];
188-
foreach ($locales as $locale) {
189-
if ($locale === 'en') {
190-
continue;
191-
}
192-
$aboutFields[] = $this->localeField('About title (' . strtoupper($locale) . ')', $locale, 'about_title', false);
193-
$aboutFields[] = $this->localeField('About description 1 (' . strtoupper($locale) . ')', $locale, 'about_description_1', true, true);
194-
$aboutFields[] = $this->localeField('About description 2 (' . strtoupper($locale) . ')', $locale, 'about_description_2', true, true);
195-
}
221+
$aboutFields[] = \Laravel\Nova\Fields\Heading::make('Other languages');
222+
$aboutFields = array_merge($aboutFields, $this->otherLocaleFields($locales, [
223+
['label' => 'About title', 'key' => 'about_title'],
224+
['label' => 'About description 1', 'key' => 'about_description_1', 'textarea' => true, 'rich' => true],
225+
['label' => 'About description 2', 'key' => 'about_description_2', 'textarea' => true, 'rich' => true],
226+
]));
196227
foreach (self::buttonKeysBySection()['about'] as $key => $title) {
197228
$aboutFields = array_merge($aboutFields, $this->buttonFields($key, $title));
198229
}
199230

231+
// — Resources: English first, then other languages, then buttons
200232
$resourcesFields = [
201233
Boolean::make('Use dynamic content for this section', 'resources_dynamic'),
202-
Text::make('Resources title (default)', 'resource_title')->nullable(),
203-
Text::make('Young person / parent title (default)', 'resource_person_title')->nullable(),
204-
Trix::make('Young person / parent description 1 (default)', 'resource_person_description_1')->nullable()->help('Use the toolbar for bold, italic, links, lists.'),
205-
Trix::make('Young person / parent description 2 (default)', 'resource_person_description_2')->nullable()->help('Use the toolbar for bold, italic, links, lists.'),
206-
Text::make('Young person / parent image (default)', 'resource_young_image')->nullable(),
207-
Text::make('Educator title (default)', 'resource_educator_title')->nullable(),
208-
Trix::make('Educator description (default)', 'resource_educator_description')->nullable()->help('Use the toolbar for bold, italic, links, lists.'),
209-
Text::make('Educator image (default)', 'resource_educator_image')->nullable(),
234+
\Laravel\Nova\Fields\Heading::make('English'),
235+
Text::make('Resources title (English)', 'resource_title')->nullable(),
236+
Text::make('Young person / parent title (English)', 'resource_person_title')->nullable(),
237+
Trix::make('Young person / parent description 1 (English)', 'resource_person_description_1')->nullable()->help('Use the toolbar for bold, italic, links, lists.'),
238+
Trix::make('Young person / parent description 2 (English)', 'resource_person_description_2')->nullable()->help('Use the toolbar for bold, italic, links, lists.'),
239+
Text::make('Young person / parent image (English)', 'resource_young_image')->nullable(),
240+
Text::make('Educator title (English)', 'resource_educator_title')->nullable(),
241+
Trix::make('Educator description (English)', 'resource_educator_description')->nullable()->help('Use the toolbar for bold, italic, links, lists.'),
242+
Text::make('Educator image (English)', 'resource_educator_image')->nullable(),
210243
];
211-
foreach ($locales as $locale) {
212-
if ($locale === 'en') {
213-
continue;
214-
}
215-
$resourcesFields[] = $this->localeField('Resource title (' . strtoupper($locale) . ')', $locale, 'resource_title', false);
216-
$resourcesFields[] = $this->localeField('Young person title (' . strtoupper($locale) . ')', $locale, 'resource_person_title', false);
217-
$resourcesFields[] = $this->localeField('Young person description 1 (' . strtoupper($locale) . ')', $locale, 'resource_person_description_1', true, true);
218-
$resourcesFields[] = $this->localeField('Young person description 2 (' . strtoupper($locale) . ')', $locale, 'resource_person_description_2', true, true);
219-
$resourcesFields[] = $this->localeField('Educator title (' . strtoupper($locale) . ')', $locale, 'resource_educator_title', false);
220-
$resourcesFields[] = $this->localeField('Educator description (' . strtoupper($locale) . ')', $locale, 'resource_educator_description', true, true);
221-
}
244+
$resourcesFields[] = \Laravel\Nova\Fields\Heading::make('Other languages');
245+
$resourcesFields = array_merge($resourcesFields, $this->otherLocaleFields($locales, [
246+
['label' => 'Resource title', 'key' => 'resource_title'],
247+
['label' => 'Young person title', 'key' => 'resource_person_title'],
248+
['label' => 'Young person description 1', 'key' => 'resource_person_description_1', 'textarea' => true, 'rich' => true],
249+
['label' => 'Young person description 2', 'key' => 'resource_person_description_2', 'textarea' => true, 'rich' => true],
250+
['label' => 'Educator title', 'key' => 'resource_educator_title'],
251+
['label' => 'Educator description', 'key' => 'resource_educator_description', 'textarea' => true, 'rich' => true],
252+
]));
222253
foreach (self::buttonKeysBySection()['resources'] as $key => $title) {
223254
$resourcesFields = array_merge($resourcesFields, $this->buttonFields($key, $title));
224255
}
225256

257+
// — Why Matters: English first, then other languages
226258
$mattersFields = [
227259
Boolean::make('Use dynamic content for this section', 'matters_dynamic'),
228-
Text::make('Section title (default)', 'matters_title')->nullable(),
229-
Text::make('Graph 1 image (default)', 'matters_graph1_image')->nullable(),
230-
Text::make('Graph 1 link (default)', 'matters_graph1_link')->nullable(),
231-
Trix::make('Graph 1 caption (default)', 'matters_graph1_caption')->nullable(),
232-
Text::make('Graph 2 image (default)', 'matters_graph2_image')->nullable(),
233-
Text::make('Graph 2 link (default)', 'matters_graph2_link')->nullable(),
234-
Trix::make('Graph 2 caption (default)', 'matters_graph2_caption')->nullable(),
235-
Text::make('Graph 3 image (default)', 'matters_graph3_image')->nullable(),
236-
Text::make('Graph 3 link (default)', 'matters_graph3_link')->nullable(),
237-
Trix::make('Graph 3 caption (default)', 'matters_graph3_caption')->nullable(),
238-
Trix::make('Paragraph 1 (default)', 'matters_paragraph_1')->nullable()->help('Use the toolbar for bold, italic, links, lists.'),
239-
Trix::make('Paragraph 2 (default)', 'matters_paragraph_2')->nullable()->help('Use the toolbar for bold, italic, links, lists.'),
260+
\Laravel\Nova\Fields\Heading::make('English'),
261+
Text::make('Section title (English)', 'matters_title')->nullable(),
262+
Text::make('Graph 1 image (English)', 'matters_graph1_image')->nullable(),
263+
Text::make('Graph 1 link (English)', 'matters_graph1_link')->nullable(),
264+
Trix::make('Graph 1 caption (English)', 'matters_graph1_caption')->nullable(),
265+
Text::make('Graph 2 image (English)', 'matters_graph2_image')->nullable(),
266+
Text::make('Graph 2 link (English)', 'matters_graph2_link')->nullable(),
267+
Trix::make('Graph 2 caption (English)', 'matters_graph2_caption')->nullable(),
268+
Text::make('Graph 3 image (English)', 'matters_graph3_image')->nullable(),
269+
Text::make('Graph 3 link (English)', 'matters_graph3_link')->nullable(),
270+
Trix::make('Graph 3 caption (English)', 'matters_graph3_caption')->nullable(),
271+
Trix::make('Paragraph 1 (English)', 'matters_paragraph_1')->nullable()->help('Use the toolbar for bold, italic, links, lists.'),
272+
Trix::make('Paragraph 2 (English)', 'matters_paragraph_2')->nullable()->help('Use the toolbar for bold, italic, links, lists.'),
240273
];
241-
foreach ($locales as $locale) {
242-
if ($locale === 'en') {
243-
continue;
244-
}
245-
$mattersFields[] = $this->localeField('Matters title (' . strtoupper($locale) . ')', $locale, 'matters_title', false);
246-
$mattersFields[] = $this->localeField('Matters paragraph 1 (' . strtoupper($locale) . ')', $locale, 'matters_paragraph_1', true, true);
247-
$mattersFields[] = $this->localeField('Matters paragraph 2 (' . strtoupper($locale) . ')', $locale, 'matters_paragraph_2', true, true);
248-
}
249-
274+
$mattersFields[] = \Laravel\Nova\Fields\Heading::make('Other languages');
275+
$mattersFields = array_merge($mattersFields, $this->otherLocaleFields($locales, [
276+
['label' => 'Matters title', 'key' => 'matters_title'],
277+
['label' => 'Matters paragraph 1', 'key' => 'matters_paragraph_1', 'textarea' => true, 'rich' => true],
278+
['label' => 'Matters paragraph 2', 'key' => 'matters_paragraph_2', 'textarea' => true, 'rich' => true],
279+
]));
280+
281+
// — FAQ: English first, then other languages, then FAQ items
250282
$faqFields = [
251283
Boolean::make('Use dynamic content for this section', 'faq_dynamic'),
252-
Text::make('FAQ section title (default)', 'faq_title')->nullable(),
284+
\Laravel\Nova\Fields\Heading::make('English'),
285+
Text::make('FAQ section title (English)', 'faq_title')->nullable(),
253286
];
254-
foreach ($locales as $locale) {
255-
if ($locale === 'en') {
256-
continue;
257-
}
258-
$faqFields[] = $this->localeField('FAQ title (' . strtoupper($locale) . ')', $locale, 'faq_title', false);
259-
}
287+
$faqFields[] = \Laravel\Nova\Fields\Heading::make('Other languages');
288+
$faqFields = array_merge($faqFields, $this->otherLocaleFields($locales, [
289+
['label' => 'FAQ title', 'key' => 'faq_title'],
290+
]));
260291
if (Schema::hasTable('girls_in_digital_faq_items')) {
261-
$faqFields[] = HasMany::make('FAQ items', 'faqItems', GirlsInDigitalFaqItem::class);
292+
$faqFields[] = HasMany::make('FAQ items', 'faqItems', GirlsInDigitalFaqItem::class)
293+
->fillUsing(function () {
294+
// Do not set faqItems on the parent; children are managed separately.
295+
});
262296
} else {
263297
$faqFields[] = \Laravel\Nova\Fields\Heading::make('Run migration to enable FAQ items: php artisan migrate --path=database/migrations/2026_02_12_100000_create_girls_in_digital_faq_items_table.php');
264298
}
265299

300+
// All sections are collapsible accordions, closed by default
266301
return [
267302
ID::make()->onlyOnForms(),
268303

0 commit comments

Comments
 (0)