tractorcow-farm / silverstripe-fluent

Multi-language translate module for Silverstripe, without having to manage separate site trees.
BSD 3-Clause "New" or "Revised" License
93 stars 111 forks source link

Member Locale overriden when fluent is used to translate a Member field #509

Open alessandromarotta opened 5 years ago

alessandromarotta commented 5 years ago

Affected Version

tractorcow/silverstripe-fluent 4.2.0 with silverstripe-framework 4.3.3

Description

I need to translate a Member field ("About Me") with Fluent but this overrides the "Locale" value of the Member, which means that it will change the "Interface Language" of the CMS.

amwMemberPrivateExtension.php

namespace alesmaweb\core\Extensions;

use SilverStripe\ORM\DataExtension;
use SilverStripe\Forms\HTMLEditor\HTMLEditorField;

class amwMemberPrivateExtension extends DataExtension {

private static $db = array(
        "AboutMe" => "HTMLText"
}

public function updateCMSFields(FieldList $fields) {
   $fields->addFieldsToTab("Root.PersonalData", array(
            HTMLEditorField::create("AboutMe", _t(__CLASS__.".AboutMe","About Me"))
   ));

   //test
   print_r($this->owner->Locale); //--> will print the current selected Locale of Fluent and not the user Locale (Interface Language of the CMS)

   print_r($this->owner->dbObject("Locale")->value); //--> will print the current selected Locale of Fluent and not the user Locale (Interface Language of the CMS), the same of "->Locale"

}

}

myExtensions.yml

SilverStripe\Security\Member:
frontend_publish_required: false
  extensions:
    - alesmaweb\core\Extensions\amwMemberPrivateExtension
    - TractorCow\Fluent\Extension\FluentExtension
  translate:
    - AboutMe

I think this is a bug, but how could I solve this problem with a workaround?

Thank you!

alessandromarotta commented 5 years ago

I found this workaround:

public function updateCMSFields(FieldList $fields) {
   [...]
   $locale_query = SQLSelect::create();
   $locale_query->setFrom('member');
   $locale_query->addWhere('ID = ' . $this->owner->ID);
   $locale_query->setSelect('Locale');
   $this->owner->setField("Locale", $locale_query->execute()->value());
}
robbieaverill commented 5 years ago

Yes, I think you've found a naming conflict between Fluent and framework. There's not much that can be done, other than ensuring that the methods Fluent adds to owner classes via its extensions are called getFluentLocale() instead of getLocale(). This would be a breaking change unfortunately, so would be unlikely to be done and released quickly.

At a slightly higher level, this problem would also exist in framework for all extensions being added to an owner object - e.g. three extensions all add the same method getFooBar(), you can't easily define which one should run and in which order.

I'm glad you've found a workaround. I think for now the best approach would be to update the documentation to include a note for this problem. If you'd be keen to make a pull request to do so, that'd be great!