This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
Greetings! Please, can anyone help me to understand how to implement custom field with translating and for particular page. I have a subject to add subtitle to every page in CMS, and add repeater gallery field to only one page.
I've created a plugin with this code:
public function register()
{
Event::listen('backend.form.extendFields', function ($widget) {
if ( ! $widget->model instanceof \Cms\Classes\Page) {
return;
}
if ( ! $widget->isNested) {
$widget->addTabFields([
'settings[subtitle]' => [
'label' => 'Subtitle',
'tab' => 'cms::lang.editor.settings',
'type' => 'text',
],
], 'primary');
}
});
}
1) How to translate this field with Translate plugin ?
2) How to add field like above, but only for particular page and type of repeater?
Thank you!
- the following code in your plugin's register() method should work:
Page::extend(function($page) { if (!$page->propertyExists('translatable')) { $page->addDynamicProperty('translatable', []); } $page->translatable = array_merge($page->translatable, ['subtitle']); });
for #2, you could check the model's fileName attribute, but that would only be available when the page has been created:
if ($widget->model->fileName === 'mypage.htm') {
$widget->addTabFields(...);
}
mjauvin said:
- the following code in your plugin's register() method should work:
Page::extend(function($page) { if (!$page->propertyExists('translatable')) { $page->addDynamicProperty('translatable', []); } $page->translatable = array_merge($page->translatable, ['subtitle']); });
Nope :( It also breaks built in "title" and "description" fields translate, but, when i add these fields to an array
$page->translatable = array_merge($page->translatable, ['title', 'description', 'subtitle']);
title and description are working now, but subtitle isn't :(
Last updated
mjauvin said:
for #2, you could check the model's fileName attribute, but that would only be available when the page has been created:
if ($widget->model->fileName === 'mypage.htm') { $widget->addTabFields(...); }
This is working! I think for my needs it is enough, thanks! Now i have to understand, why your code for translating isn't working for me :(
Looks like only fields already in the base model can currently be translated since RainLab.Translate uses the backend.form.extendFieldsBefore
event in order to replace the form fields to use their MultiLang equivalent formwidgets.
I found how to do this, the field needs to be created using the extendFieldsBefore
event like here:
Event::listen('backend.form.extendFieldsBefore', function($widget) {
if (!($widget->model instanceof \Cms\Classes\Page && $widget->getController() instanceof \Cms\Controllers\Index)) {
return;
}
$widget->tabs['fields']['settings[subtitle]'] = [
'label' => 'Subtitle',
'tab' => 'cms::lang.editor.settings',
];
});
You also need to add this to your plugin class:
public $require = ['RainLab.Translate'];
And use this code to add the translatable property:
public function register()
{
\Cms\Classes\Page::extend(function($page) {
$page->translatable = array_merge($page->translatable, ['subtitle']);
});
}
1-8 of 8