October CMS resources and help articles

Simple and to the point. Optimized by the community.

How to translate fields added dynamically to a Model (or CMS Page)

4
by mjauvin, last modified on September 15th, 2020

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');
   }
});

Discussion

1 comment

0
zborima14
Post on July 20th, 2020 1:18 PM

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.

We use cookies to measure the performance of this website. Do you want to accept these cookies?