Make custom specified data fields to share the same value across different locale/language versions.
Jarkko Linnanvirta (Nickname: Taitava)
posti (at) taitavasti.fi
This module makes it possible to define fields that cannot be translated to different languages when using the
Translatable module. For example, you may have a model called Person with two fields: name and description. You have
installed the Translatable module in order to be able to provide the description field in different languages. But since
a person's name is not (usually) translated, there is no reason to have to enter the person's name multiple times - once
per every translation.
In practice, every time an object (that extends this module) gets saved to the database, its translations are iterated
over and the values of fields marked as untranslatable are copied over to the other translations and saved.
The CMS module is not necessarily needed.
This is a very simple one to configure. But first, either download this module and unpack it to a folder named
"untranslatablefields" under your site's root directory, or install via Composer:
composer require "taitava/silverstripe-untranslatablefields:*"
Then create a new file: mysite/_config/untranslatablefields.yml and put the following content there:
--- UntranslatableFields: fields: MyClass: - MyField - CommonField AnotherClass - AnotherField - CommonField
Also, remember to extend your classes with this module. Edit mysite/_config/config.yml and add the following lines:
MyClass: extensions: - UntranslatableFieldsExtension AnotherClass: extensions: - UntranslatableFieldsExtension
These two configurations together render fields MyField and CommonField in MyClass and fields AnotherField and
CommonField untranslatable. Note that specifying the classes does not affect any child classes!
You can also do the opposite: Untranslate all but the specified fields! Just add
invert: true to
--- UntranslatableFields: fields: MyClass: - MyField - CommonField AnotherClass - AnotherField - CommonField invert: true
If the user saves and publishes for example a Page object, its different language versions will get saved, but not
published by default. This is because the DataObject::write() method does not publish changes by default.
UntranslatableFields can be set to do the publishing for DataObjects that has the Versioned extension.
This feature is off by default. To turn it on, add the following to mysite/_config/untranslatablefields.yml:
UnstranslatableFields: auto_publish: true
Note: if the translation was in the Stage mode (= not published) before the write operation, it won't get published
automatically even if this option is set to true. If there were an oldder published version, the changes are written to
the newest (Staged) version and will not get published.
There are a couple of low level ways to give the CMS users a hint about untranslatable fields. This should also work in
the general backend even without the CMS module and in the ModelAdmin of DataObjects.
You can add some custom CSS classes to the FormField elemnts of all untranslatable fields. Add this to
UntranslatableFields: add_classes_to_cms: - untranslatable - 'another-class'
You can also set HTML attributes in the same way:
UntranslatableFields: add_attributes_to_cms: - data-untranslatable: yes - title: "This field can't be translated separately. Changes to this field are saved across all existing translations."
Just for you to know: if you are untranslating fields that has no CMS fields, this feature won't interfere with them: the
module only tries to add these classees/attributes to those fields that exist in the CMS/ModelAdmin editor.
Stuff that could be done:
Module rating system helping users find modules that are well supported. For more on how the rating system works visit Module standards
Score not correct? Let us know there is a problem