Automatically make languages live/remove user control

Use case - on a bilingual site I need to make both configured languages live by default for any item (including settings). I'd also ideally like to remove the ability for admin users to edit this at all. I've spotted a variable called $controlLanguagesPublication in the form layout which seems to hide the language active controls, and am using View::share in AppServiceProvider to globally force this to false . However this seems slightly brutal and I'm not sure if there might be any side effects. Also this doesn't solve selecting all languages as active by default (although I guess I could probably figure that out during save) Could anyone possibly confirm the best way to achieve this?
1 Reply
moussaclarke
moussaclarkeOP3mo ago
For selecting all languages as active automatically I've wound up with some logic in prepareFieldsBeforeCreate on all repositories. In case this helps anyone here is the code for my solution. And if anyone knows of a better/cleaner way please comment 👍
// in AppServiceProvider boot() method
View::share('controlLanguagesPublication', false);

// in relevant model repositories - I used a trait as I needed it in all of them
public function prepareFieldsBeforeCreate($fields): array
{
foreach ($fields['languages'] as $key => $language) {
$fields['languages'][$key]['published'] = true;
}

return parent::prepareFieldsBeforeCreate($fields); // @phpstan-ignore-line
}
// in AppServiceProvider boot() method
View::share('controlLanguagesPublication', false);

// in relevant model repositories - I used a trait as I needed it in all of them
public function prepareFieldsBeforeCreate($fields): array
{
foreach ($fields['languages'] as $key => $language) {
$fields['languages'][$key]['published'] = true;
}

return parent::prepareFieldsBeforeCreate($fields); // @phpstan-ignore-line
}
Want results from more Discord servers?
Add your server