How to translate fields added dynamically to a Model (or CMS Page)
0
Since the Winter.Translate
plugin uses the backend.form.extendFieldsBefore
event to replace regular form fields with their multi-lang equivalent, new fields must be added using this event as well, otherwise they won't be translatable.
In your plugin's boot()
method, create the new field and add it to the model's translatable property array:
Event::listen('backend.form.extendFieldsBefore', function($widget) {
$widget->config['fields']['myField'] =
$widget->tabs['fields']['myField'] = [
'label' => 'My New Field',
'type' => text,
'tab' => 'myTab'
];
});
YourModel::extend(function($model) {
if (!$model->propertyExists('translatable')) {
$model->addDynamicProperty('translatable', []);
}
$model->translatable = array_merge($model->translatable, ['myField']);
if (!$model->isClassExtendedWith('Winter.Translate.Behaviors.TranslatableModel')) {
$model->extendClassWith('Winter.Translate.Behaviors.TranslatableModel');
}
});
There are no comments yet
Be the first one to comment