Permalink
Browse files

NEW Make it easier to show original values for custom fields.

  • Loading branch information...
1 parent 9454eb3 commit 44f8180110c37e8b926559e5402b8f9130f291ec @drzax drzax committed with chillu Nov 2, 2012
Showing with 83 additions and 21 deletions.
  1. +34 −3 code/model/Translatable.php
  2. +20 −17 docs/en/index.md
  3. +29 −1 tests/unit/TranslatableTest.php
@@ -214,6 +214,14 @@ class Translatable extends DataExtension implements PermissionProvider {
protected static $allowed_locales = null;
/**
+ * @var array An array of fields which should be excluded from
+ * being transformed in the CMS for translated dataobjects. This
+ * includes some default excludes and any field which has already
+ * been transformed.
+ */
+ protected $translatableExcludes = null;
+
+ /**
* Reset static configuration variables to their default values
*/
static function reset() {
@@ -927,6 +935,14 @@ public function alternateGetByLink($URLSegment, $parentID) {
//-----------------------------------------------------------------------------------------------//
+ function applyTranslatableFieldsUpdate($fields, $type) {
+ if (method_exists($this, $type)) {
+ $this->$type($fields);
+ } else {
+ throw new InvalidArgumentException("Method $type does not exist on object of type ". get_class($this));
+ }
+ }
+
/**
* If the record is not shown in the default language, this method
* will try to autoselect a master language which is shown alongside
@@ -944,6 +960,8 @@ public function alternateGetByLink($URLSegment, $parentID) {
*/
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"
@@ -996,13 +1014,17 @@ function updateCMSFields(FieldList $fields) {
$langDropdown->addExtraClass('languageDropdown no-change-track');
$createButton->addExtraClass('createTranslationButton');
+
+ $this->owner->translatableFieldsAdded = true;
+
}
function updateSettingsFields(&$fields) {
$this->addTranslatableFields($fields);
}
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));
@@ -1023,8 +1045,15 @@ protected function addTranslatableFields(&$fields) {
'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
@@ -1059,7 +1088,7 @@ protected function addTranslatableFields(&$fields) {
// (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
@@ -1068,6 +1097,8 @@ protected function addTranslatableFields(&$fields) {
// 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()) {
@@ -1081,7 +1112,7 @@ protected function addTranslatableFields(&$fields) {
)
)
);
- }
+ }
}
/**
View
@@ -197,10 +197,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 {
@@ -214,24 +214,27 @@ attach this behaviour to custom fields by using Translatable_Transformation as s
// Add fields as usual
$additionalField = new TextField('AdditionalProperty');
- $fields->addFieldToTab('Root.Content.Main', $additionalField);
+ $fields->addFieldToTab('Root.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, 'updateCMSFields');
return $fields;
}
-
- }
+ function getSettingsFields() {
+ $fields = parent::getSettingsFields();
+
+ // Add fields as usual
+ $additionalField = new TextField('AdditionalProperty');
+ $fields->addFieldToTab('Root.Main', $additionalField);
+
+ // Apply Translatable modifications
+ $this->applyTranslatableFieldsUpdate($fields, 'updateSettingsFields');
+
+ return $fields;
+ }
+ }
### Translating the Homepage
@@ -220,7 +220,8 @@ function testTranslationCantHaveSameURLSegmentAcrossLanguages() {
}
function testUpdateCMSFieldsOnSiteTree() {
- $pageOrigLang = $this->objFromFixture('Page', 'testpage_en');
+ $pageOrigLang = new TranslatableTest_Page();
+ $pageOrigLang->write();
// first test with default language
$fields = $pageOrigLang->getCMSFields();
@@ -233,6 +234,11 @@ function testUpdateCMSFieldsOnSiteTree() {
$fields->dataFieldByName('Title_original'),
'Translatable doesnt modify fields if called in default language (e.g. "non-translation mode")'
);
+ $this->assertInstanceOf(
+ 'TextField',
+ $fields->dataFieldByName('TranslatableProperty'),
+ 'Has custom field'
+ );
// then in "translation mode"
$pageTranslated = $pageOrigLang->createTranslation('fr_FR');
@@ -248,6 +254,16 @@ function testUpdateCMSFieldsOnSiteTree() {
$fields->dataFieldByName('Title_original'),
'Translatable adds the original value as a ReadonlyField in "translation mode"'
);
+ $this->assertInstanceOf(
+ 'ReadonlyField',
+ $fields->dataFieldByName('TranslatableProperty_original'),
+ 'Retains original custom field'
+ );
+ $this->assertInstanceOf(
+ 'TextField',
+ $fields->dataFieldByName('TranslatableProperty'),
+ 'Adds custom fields as ReadonlyField'
+ );
}
@@ -990,6 +1006,18 @@ class TranslatableTest_Page extends Page implements TestOnly {
static $db = array(
'TranslatableProperty' => 'Text'
);
+
+ function getCMSFields() {
+ $fields = parent::getCMSFields();
+ $fields->addFieldToTab(
+ 'Root.Main',
+ new TextField('TranslatableProperty')
+ );
+
+ $this->applyTranslatableFieldsUpdate($fields, 'updateCMSFields');
+
+ return $fields;
+ }
}
DataObject::add_extension('TranslatableTest_DataObject', 'TranslatableTest_Extension');

0 comments on commit 44f8180

Please sign in to comment.