tractorcow-farm / silverstripe-fluent

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

Additional page fields overwrite each other in different localizations #864

Closed BrandSven closed 2 months ago

BrandSven commented 2 months ago

Module version(s) affected

7.1.2

Description

In our project, we added a new field "Heading" to a page and made it translateable (see below).

We create a new page in the default language and enter the text "default language" into the field "Heading".

Then we create a new draft of that page in a second language, entering "second language" into the field "Heading".

Upon saving the translated page, the default (!) language page has "second language" written in the field "Heading". If we change the field in the default language (back to "default language"), the field in the second (!) language reads "default language".

It seems like somehow the field "Heading" is shared between the two languages. Both languages will always the last saved value from any of the single languages.

The default translateable page fields like Title, MetaDescription etc. are translated correctly and don't display this odd behaviour. Only the new page fields we added behave erroneously.

Is this a bug in the plugin, or a misconfiguration on our part?

Page.php:

<?php

use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Forms\TextField;

class Page extends SiteTree
{
    private static array $db = [
        "Heading" => "Text"
    ];

    private static array $translate = [
        "Heading"
    ];

    public function getCMSFields()
    {
        $fields = parent::getCMSFields();
        $fields->addFieldsToTab("Root.Header", [
            TextField::create("Heading", "Überschrift")
        ]);
        return $fields;
    }
}

How to reproduce

  1. Install SS5 with Fluent 7.1.2
  2. Setup a default language and any second language
  3. Add a new text field to the default page (see code below)
  4. dev/build
  5. Create a new page in the default language. Enter "default" in the new Text field and save the page
  6. Create a translated draft page in the secondary language. Enter "secondary" in the new Text field and save the draft
  7. Switch back to the default language. The text in the new field has magically transformed from "default" to "secondary"!

Page.php:

<?php

use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Forms\TextField;

class Page extends SiteTree
{
    private static array $db = [
        "Heading" => "Text"
    ];

    private static array $translate = [
        "Heading"
    ];

    public function getCMSFields()
    {
        $fields = parent::getCMSFields();
        $fields->addFieldsToTab("Root.Header", [
            TextField::create("Heading", "Überschrift")
        ]);
        return $fields;
    }
}

Possible Solution

No response

Additional Context

No response

Validations

BrandSven commented 2 months ago

Turns out it was a misconfiguration on our part, not an issue in the module.

silverstripesk commented 2 months ago

@BrandSven what was the reason? We have similar isseus.

BrandSven commented 2 months ago

The reason were duplicate field names due to inheritance.

Page extends SiteTree: Field "Heading"

ElementalPage extends Page: no fields

ContactPage extends ElementalPage: Field "Heading" (again)

Bad idea...

Once we renamed the field in ContactPage to something else ("ContactHeading"), the translation handling worked as expected.