mirror of
https://github.com/silverstripe/silverstripe-translatable
synced 2024-10-22 09:05:59 +00:00
FIXED: Issue with siteconfig translation incorrectly ignoring canTranslate permissions and crashing
UPDATED: Cleanup and refactoring to make more readable UPDATED: Translation of siteconfig now better handles singleton(SiteConfig) UPDATED: Renamed populateSiteConfig to populateSiteConfigDefaults to better reflect its purpose FIXED: Parameter typing on getStagingSiteConfig bla
This commit is contained in:
parent
67915ea695
commit
a44546bcdd
@ -1142,41 +1142,40 @@ class Translatable extends DataExtension implements PermissionProvider {
|
|||||||
* This function DOES populate the ID field with the newly created object ID
|
* This function DOES populate the ID field with the newly created object ID
|
||||||
* @see SiteConfig
|
* @see SiteConfig
|
||||||
*/
|
*/
|
||||||
protected function populateSiteConfig() {
|
protected function populateSiteConfigDefaults() {
|
||||||
|
|
||||||
// Work-around for population of defaults during database initialisation
|
// Work-around for population of defaults during database initialisation.
|
||||||
// When the database is being setup singleton('SiteConfig') is called.
|
// When the database is being setup singleton('SiteConfig') is called.
|
||||||
// Singleton objects should not call populateDefault() but a bug seems to be allowing it
|
|
||||||
// to happen anyway. Looking up existing SiteConfig objects without a database crashes horribly
|
|
||||||
// @todo Remove this hack once this bug is fixed
|
|
||||||
if(!DB::getConn()->hasTable($this->owner->class)) return;
|
if(!DB::getConn()->hasTable($this->owner->class)) return;
|
||||||
|
|
||||||
// Find the best base translation for site config
|
// Find the best base translation for SiteConfig
|
||||||
Translatable::disable_locale_filter();
|
Translatable::disable_locale_filter();
|
||||||
$existingConfig = SiteConfig::get()->filter(array('Locale' => Translatable::default_locale()))->first();
|
$existingConfig = SiteConfig::get()->filter(array('Locale' => Translatable::default_locale()))->first();
|
||||||
if(!$existingConfig) $existingConfig = SiteConfig::get()->first();
|
if(!$existingConfig) $existingConfig = SiteConfig::get()->first();
|
||||||
Translatable::enable_locale_filter();
|
Translatable::enable_locale_filter();
|
||||||
|
|
||||||
// Base case; creation of the first site config
|
// Stage this SiteConfig and copy into the current object
|
||||||
if(!$existingConfig) {
|
if(
|
||||||
$this->owner->Locale = Translatable::get_current_locale();
|
$existingConfig
|
||||||
return;
|
// Double-up of SiteConfig in the same locale can be ignored. Often caused by singleton(SiteConfig)
|
||||||
|
&& !$existingConfig->getTranslation(Translatable::get_current_locale())
|
||||||
|
// If translation is not allowed by the current user then do not
|
||||||
|
// allow this code to attempt any behind the scenes translation.
|
||||||
|
&& $existingConfig->canTranslate(null, Translatable::get_current_locale())
|
||||||
|
) {
|
||||||
|
// Create an unsaved "staging" translated object using the correct createTranslation mechanism
|
||||||
|
$stagingConfig = $existingConfig->createTranslation(Translatable::get_current_locale(), false);
|
||||||
|
$this->owner->update($stagingConfig->toMap());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Edge case: creating new translation in same locale as an existing object probably
|
// Maintain single translation group for SiteConfig
|
||||||
// should not have the same translation group (@todo find examples)
|
if($existingConfig) {
|
||||||
if(Translatable::get_current_locale() == $existingConfig->Locale) return;
|
|
||||||
|
|
||||||
// Create a third unsaved "staging" translated object using the correct createTranslation mechanism
|
|
||||||
$stagingConfig = $existingConfig->createTranslation(Translatable::get_current_locale(), false);
|
|
||||||
|
|
||||||
// Copy fields from translated object to the generated object.
|
|
||||||
// This is similar to the creation of objects via createTranslation,
|
|
||||||
// although by default this object should not be saved
|
|
||||||
$this->owner->update($stagingConfig->toMap());
|
|
||||||
$this->owner->_TranslationGroupID = $existingConfig->getTranslationGroup();
|
$this->owner->_TranslationGroupID = $existingConfig->getTranslationGroup();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->owner->Locale = Translatable::get_current_locale();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Enables automatic population of SiteConfig fields using createTranslation if
|
* Enables automatic population of SiteConfig fields using createTranslation if
|
||||||
* created outside of the Translatable module
|
* created outside of the Translatable module
|
||||||
@ -1192,7 +1191,7 @@ class Translatable extends DataExtension implements PermissionProvider {
|
|||||||
{
|
{
|
||||||
// Use enable_siteconfig_generation to prevent infinite loop during object creation
|
// Use enable_siteconfig_generation to prevent infinite loop during object creation
|
||||||
self::$enable_siteconfig_generation = false;
|
self::$enable_siteconfig_generation = false;
|
||||||
$this->populateSiteConfig();
|
$this->populateSiteConfigDefaults();
|
||||||
self::$enable_siteconfig_generation = true;
|
self::$enable_siteconfig_generation = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user