Winter 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)

0
by mjauvin, last modified on August 9th, 2022 - Previously published on OctoberTricks

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

Discussion

0 comments

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