Skip to content

Commit 4f61360

Browse files
authored
Update README.md (#24)
More information about extending a plugin with translatable fields
1 parent 7c00c82 commit 4f61360

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

README.md

+41
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,47 @@ This plugin activates a feature in the CMS that allows Mail template files to us
129129
* **mail-notify-ru.htm** will contain the mail template in Russian.
130130
* **mail-notify-fr.htm** will contain the mail template in French.
131131

132+
## Extending a plugin with translatable fields
133+
If you are extending a plugin and want the added fields in the backend to be translatable, you have to use the '[backend.form.extendFieldsBefore](https://wintercms.com/docs/events/event/backend.form.extendFieldsBefore)' and tell which fields you want to be translatable by pushing them to the array.
134+
135+
```
136+
public function boot() {
137+
Event::listen('backend.form.extendFieldsBefore', function($widget) {
138+
139+
// Only apply this listener when the Page controller is being used
140+
if (!$widget->getController() instanceof \Winter\Pages\Controllers\Index) {
141+
return;
142+
}
143+
144+
// Only apply this listener when the Page model is being modified
145+
if (!$widget->model instanceof \Winter\Pages\Classes\Page) {
146+
return;
147+
}
148+
149+
// Only apply this listener when the Form widget in question is a root-level
150+
// Form widget (not a repeater, nestedform, etc)
151+
if ($widget->isNested) {
152+
return;
153+
}
154+
155+
// Add fields
156+
$widget->tabs['fields']['viewBag[myField]'] = [
157+
'tab' => 'mytab',
158+
'label' => 'myLabel',
159+
'type' => 'text'
160+
];
161+
162+
// Translate fields
163+
$translatable = [
164+
'viewBag[myField]'
165+
];
166+
167+
// Merge the fields in the translatable array
168+
$widget->model->translatable = array_merge($widget->model->translatable, $translatable);
169+
170+
});
171+
}
172+
```
132173
## Model translation
133174

134175
Models can have their attributes translated by using the `Winter.Translate.Behaviors.TranslatableModel` behavior and specifying which attributes to translate in the class.

0 commit comments

Comments
 (0)