How to translate fields added dynamically to a Model (or CMS Page)
4
Since the RainLab.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->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('RainLab.Translate.Behaviors.TranslatableModel')) {
$model->extendClassWith('RainLab.Translate.Behaviors.TranslatableModel');
}
});
Hello, I followed the instructions and I added new tab fields. Unfortunately it can't be translated by Translation model. So I can't switch languages in fields to translate fields.