Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong locale field value on DataObject #190

Closed
suntrop opened this issue May 8, 2015 · 8 comments
Closed

Wrong locale field value on DataObject #190

suntrop opened this issue May 8, 2015 · 8 comments

Comments

@suntrop
Copy link

suntrop commented May 8, 2015

I've got a custom SiteConfig extension. The hidden field 'Locale' ist always the default locale, not the current locale.

Why isn't the value updated to the current locale? Can't save new items to other locales.

@silverstripe-issues
Copy link

Code sample please

On 08.05.2015, at 22:06, suntrop notifications@github.com wrote:

I've got a custom SiteConfig extension. The hidden field 'Locale' ist always the default locale, not the current locale.

Why isn't the value updated to the current locale? Can't save new items to other locales.


Reply to this email directly or view it on GitHub.

@Taitava
Copy link

Taitava commented Nov 17, 2015

I think I have something like this too. My case involves also the SilverStripe Blog module. The Blog module also uses silverstripe/lumberjack module to better manage the site tree of blog articles in the CMS. This module comes by default with the blog module, so it's not something I have included myself. Thus, I don't know if the Lumberjack module relates to this bug at all, but I just mentioned it just in case, because I don't know it very well.

So, what's the problem in practise? I have a site with two languages, English and Finnish, and English is the default language. I have one Blog page that is translated to both English and Finnish - that works well. While having English selected as the content language (in the dropdown field left above the site tree in the editor), if I create a new BlogPost under the Blog page, saving and publishing it goes well. But, if I want to write a blog article in Finnish, I switch the language from the dropdown field to Finnish and then create a new BlogPost page under the Blog page. I write the article and publish it. It saves the article with a wrong locale field value, 'en_US' instead of 'fi_FI'. I checked that directly from the database.

More strange, my new Finnish blog article won't appear in the CMS at all - no matter what language I select. It just doesn't show up in the gridfield that lists the blog articles. However, it does appear in the public site - but of course under the English side of it. Too bad for people who can't speak Finnish! :D

I have SilverStripe 3.1.12. Blog and Translatable are the newest versions - I have ran composer update today and don't have any version limits in composer.json for those two modules. Do you need any code samples? I think I don't have made any custom code relating to Translatable or Blog.

Edit: I just updated to SilverStripe 3.1.16. Nothing changed. I also tried to update to 3.2.1, but due to some dependencies, Composer doesn't let me update to newer than 3.1, so I can't test the newest version of SilverStripe. But perhaps that isn't needed. We'll see :).

@Taitava
Copy link

Taitava commented Nov 23, 2015

Here is a temporary fix in a form of an extension, so no need to hack any existing files:

mysite/code/BlogPostLanguageFix.php:

<?php
/**
 * For some reason the value of the Locale field of BlogPost objects do not reflect the locale that is currently
 * selected in the CMS, but instead always the default locale. This is a quick and dirty way to fix it. This extension
 * checks every time a BlogPost objects gets saved that its Locale field has the same value as what is currently
 * selected in the CMS editor, and if not, fixes the value.
 * The use of this extension is not recommended for a long term solution (because it does not address the original bug),
 * but it is instead a quick fix to put down the fire. I recommend you to remove this after someone fixes the bug. :)
 *
 * Author: Jarkko Linnanvirta / IT-Palvelu Taitava (GitHub @Taitava)
 */
class BlogPostLanguageFix extends DataExtension
{
    public function onBeforeWrite()
    {
        parent::onBeforeWrite();
        $locale = Translatable::get_current_locale();
        if ($this->owner->Locale !== $locale) $this->owner->Locale = $locale;
    }
}

Add to mysite/_config/config.yml:

BlogPost:
  extensions:
    - BlogPostLanguageFix

Please note that this does not solve the original bug, but rather fixes the symptom in a quick and dirty way. I have done very little testing after creating this extension. I created new BlogPosts in Finnish and in English and checked in the database that the locales are now saved correctly. I found no errors. If you use this fix, can you please report back here if it worked for you, and also of course if it didn't? Tell me also what locales you are using. Thank you! :)

@riddler7
Copy link

Ran into the same problem, looks to be a little more extensive. Happens whenever I try to add a new page (e.g. with the "New page" button above the site tree).

Looks like the issue is Translatable sets the default for Locale to the default locale (likely english). The onBeforeWrite only sets the Locale to the current locale if Locale isn't already set (which it was by the default value).

@Taitava
Copy link

Taitava commented Dec 12, 2016

Sounds reasonable. @silverstripe-issues , is this information enough for inspecting the issue? If still needed, I think I can write some example code showing the bug.

@dhensby
Copy link
Contributor

dhensby commented Dec 12, 2016

I'm not sure who @silverstripe-issues is (@chillu?), @Taitava and you supply some code just for clarity's sake? thanks

@Taitava
Copy link

Taitava commented Dec 12, 2016

Yes, just for clarity if needed :).

@maxime-rainville
Copy link
Contributor

Unfortunately, Silverstripe CMS 3 has entered limited support in June 2018. This means we'll only be fixing critical bugs and security issues for Silverstripe CMS 3 going forward.

You can read the Silverstripe CMS Roadmap for more information on our support commitments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants