/** * Alter the CMS Fields in order to automatically present the * correct ones based on current language. * @inheritdoc */ public function updateCMSFields(FieldList $fields) { parent::updateCMSFields($fields); if (!isset(self::$collectorCache[$this->owner->class])) { return; } // remove all localized fields from the list (generated through scaffolding) foreach (self::$collectorCache[$this->owner->class] as $translatableField => $type) { $fields->removeByName($translatableField); } // check if we're in a translation if (Translatable::default_locale() != Translatable::get_current_locale()) { /** @var TranslatableFormFieldTransformation $transformation */ $transformation = TranslatableFormFieldTransformation::create($this->owner); // iterate through all localized fields foreach (self::$collectorCache[$this->owner->class] as $translatableField => $type) { if (strpos($translatableField, Translatable::get_current_locale())) { $basename = $this->getBasename($translatableField); if ($field = $this->getLocalizedFormField($basename, Translatable::default_locale())) { $fields->replaceField($basename, $transformation->transformFormField($field)); } } } } }
/** * Convenience method to use within a locale context. * Eg. by specifying the edit fields with the UploadField. * <code> * $imageUpload = UploadField::create('Image'); * $imageUpload->setFileEditFields('getUploadEditorFields'); * </code> * @return FieldList */ public function getUploadEditorFields() { /** @var FieldList $fields */ $fields = FieldList::create(); $translatedFields = TranslatableDataObject::get_localized_class_fields($this->owner->class); $transformation = null; $defaultLocale = Translatable::default_locale(); if ($defaultLocale != Translatable::get_current_locale()) { /** @var TranslatableFormFieldTransformation $transformation */ $transformation = TranslatableFormFieldTransformation::create($this->owner); } foreach ($translatedFields as $fieldName) { // create the field in the default locale /** @var FormField $field */ $field = $this->owner->getLocalizedFormField($fieldName, $defaultLocale); // use translated title if available $field->setTitle(_t('File.' . $fieldName, $fieldName)); // if not in the default locale, we apply the form field transformation to the field if ($transformation) { $field = $transformation->transformFormField($field); } $fields->push($field); } return $fields; }