Make it easier to show original values for custom fields.

This commit is contained in:
Simon Elvery 2012-11-02 18:52:20 +10:00
parent 1bb392c8dc
commit 7cdcab00c9
2 changed files with 30 additions and 19 deletions

View File

@ -926,6 +926,12 @@ class Translatable extends DataExtension implements PermissionProvider {
//-----------------------------------------------------------------------------------------------//
function applyTranslatableFieldsUpdate($fields, $type) {
if (method_exists($this, $type)) {
$this->$type($fields);
}
}
/**
* If the record is not shown in the default language, this method
* will try to autoselect a master language which is shown alongside
@ -943,6 +949,8 @@ class Translatable extends DataExtension implements PermissionProvider {
*/
function updateCMSFields(FieldList $fields) {
$this->addTranslatableFields($fields);
if ($this->owner->translatableFieldsAdded) return;
// Show a dropdown to create a new translation.
// This action is possible both when showing the "default language"
@ -995,6 +1003,9 @@ class Translatable extends DataExtension implements PermissionProvider {
$langDropdown->addExtraClass('languageDropdown no-change-track');
$createButton->addExtraClass('createTranslationButton');
$this->owner->translatableFieldsAdded = true;
}
function updateSettingsFields(&$fields) {
@ -1002,6 +1013,7 @@ class Translatable extends DataExtension implements PermissionProvider {
}
protected function addTranslatableFields(&$fields) {
// used in LeftAndMain->init() to set language state when reading/writing record
$fields->push(new HiddenField("Locale", "Locale", $this->owner->Locale));
@ -1022,8 +1034,15 @@ class Translatable extends DataExtension implements PermissionProvider {
'ViewerGroups',
'EditorGroups',
'CanViewType',
'CanEditType'
'CanEditType',
'NewTransLang',
'createtranslation'
);
$this->translatableExcludes = ( isset($this->translatableExcludes) && is_array($this->translatableExcludes) )
? array_merge($this->translatableExcludes, $excludeFields)
: $excludeFields;
// if a language other than default language is used, we're in "translation mode",
// hence have to modify the original fields
@ -1058,7 +1077,7 @@ class Translatable extends DataExtension implements PermissionProvider {
// (fields are object references, so we can replace them with the translatable CompositeField)
foreach($allDataFields as $dataField) {
if($dataField instanceof HiddenField) continue;
if(in_array($dataField->getName(), $excludeFields)) continue;
if(in_array($dataField->getName(), $this->translatableExcludes)) continue;
if(in_array($dataField->getName(), $translatableFieldNames)) {
// if the field is translatable, perform transformation
@ -1067,6 +1086,8 @@ class Translatable extends DataExtension implements PermissionProvider {
// else field shouldn't be editable in translation-mode, make readonly
$fields->replaceField($dataField->getName(), $dataField->performReadonlyTransformation());
}
$this->translatableExcludes[] = $dataField->getName();
$this->translatableExcludes[] = $dataField->getName() . '_original';
}
} elseif($this->owner->isNew()) {
@ -1080,7 +1101,7 @@ class Translatable extends DataExtension implements PermissionProvider {
)
)
);
}
}
}
/**

View File

@ -193,10 +193,10 @@ Keep in mind that the `[api:Translatable]` extension currently doesn't support t
translated - all custom properties will automatically be fetched from their translated record on the database. This means
you don't have to explicitly mark any custom properties as being translatable.
The `[api:Translatable]` decorator applies only to the getCMSFields() method on DataObject or SiteTree, not to any fields
added in overloaded getCMSFields() implementations. See Translatable->updateCMSFields() for details. By default, custom
fields in the CMS won't show an original readonly value on a translated record, although they will save correctly. You can
attach this behaviour to custom fields by using Translatable_Transformation as shown below.
The `[api:Translatable]` decorator applies only to the getCMSFields() method on DataObject or SiteTree and the getSettingsFields()
on SiteTree, not to any fields added in overloaded getCMSFields() implementations. See Translatable->updateCMSFields() for details.
By default, custom fields in the CMS won't show an original readonly value on a translated record, although they will save correctly. You can
attach this behaviour to custom fields by calling a helper function from your getCMSFields() and getSettingsFields() functions.
:::php
class Page extends SiteTree {
@ -212,24 +212,14 @@ attach this behaviour to custom fields by using Translatable_Transformation as s
$additionalField = new TextField('AdditionalProperty');
$fields->addFieldToTab('Root.Content.Main', $additionalField);
// If a translation exists, exchange them with
// original/translation field pairs
$translation = $this->getTranslation(Translatable::default_locale());
if($translation && $this->Locale != Translatable::default_locale()) {
$transformation = new Translatable_Transformation($translation);
$fields->replaceField(
'AdditionalProperty',
$transformation->transformFormField($additionalField)
);
}
// Apply Translatable modifications
$this->applyTranslatableFieldsUpdate($fields, str_replace('get', 'update', __FUNCTION__));
return $fields;
}
}
### Translating the Homepage
Every homepage has a distinct URL, the default language is /home, a German translation by default would be /home-de_DE.