Multi-Language - Selecting a draft by defaultlanguage (or disable multilanguage for certain drafts)


#1

i am creating drafts via createchild method and while i wish to only work with the default language on that certain blueprint, drafts are created in the default language, as well as the active language.

I also changed my code that all kinds of update / create function have the kirby()->defaultLanguage->code() parameter (updating seems to work just fine) however an active language version of the draft still gets created.

On the second step i am using methods such as

$page->draft($id)

to select a draft, but it seems it’s only possible to select the draft in the current language… it doesn’t seem to accept the language parameter.

Is there some way, to make sure only having a draft in the default language? or being able to select a default language draft? Or exclude a whole template/blueprint from using translated files? I am aware that there’s the translated: false option for single fields…


#2

Have a look at the singleLanguage option. It disables language changes for any page status though and is intended to catch panel created pages. Not sure about drafts created in the backend

*Edit
Your question about selecting drafts in the right language: I am using $kirby->setCurrentLanguage($languageCode); before the $page->... call. This method is not in the reference but you’ll find it here. To not mess with the users displayed languages you should reset it to the initial value at the end of your hook.


#3

It seems it’s selecting the draft just fine now.
i am using it in a function so it also seems like i don’t have to reset the initial value

kirby()->setCurrentLanguage(kirby()->defaultLanguage()->code());
$page = $monthPage->draft($draft);

Edit:
While the above works, i still have the issue about resetting the setCurrentLanguage back to normal.

In my case i want to use this once to select a draft in this certain language, but reset it after i got the $page object and continue with the active language in the rest of the code.

function functionName($draft){
                //....
                $currentlang = kirby()->language()->code();
                kirby()->setCurrentLanguage(kirby()->defaultLanguage()->code());
                $page = $monthPage->draft($draft);
                // Sets the language to the default one as intended.

               // if i were to use below line to reset back to active language
              // kirby()->setCurrentLanguage($currentlang); 
             // the page which gets returned will also change back to currentlang
      return $page;
}

#4

Are you just looking for the content or are more page methods involved?

If it is just about content, you could try to define the language you want just for content (untested):

function functionName($draft){
    if($page = $monthPage->draft($draft)) {
       $content = $page->content(kirby()->defaultLanguage()->code());
    }
    return $content;
}

#5

this returns a page object which is used to manipulate that draft later on
with

  • update()
  • changeStatus()

so i guess $content is not enough? or could i build the page object from the content, even if this takes one additional step?


#6

I am not sure what you mean by building the page object from the content.

To keep a page “single language” in a multi language site it should be enough to focus on the update methods because

  • Kirby creates pages always in default language
  • Kirby reads the default language if no other language exists

So if you update the page always with

$page->update([...], $kirby->defaultLanguage()->code()) 

the issues with reading should be solved automatically (or actually not exist).

I don’t see where that comes from. It works in my setup. Are you updating the page after creating it without setting a language code? If you always update like above, this should not happen.


#7

I have

$page->update([
 // ....
    ], kirby()->defaultLanguage()->code());

everywhere. And it’s updating alright.

The problem is, when the draft is created, there’s

draft.en.txt
draft.de.txt (default)

no setdefaultlanguage modification

my code is apparently reading the current language, so the draft object is: draft.en.txt

while everything gets saved to draft.de.txt, the english file draft.en.txt will not validate though my other functions (e.g. checking if a field value has been set because it’s reading the english file…)

setting setdefaultlanguage without re-init

if, as talked above, setting setDefaultlanguage to DE, there’s only draft.de.txt and updating and whatsoever works, but then there’s the issue, that e.g. $page->url(kirby()->language()->code()) links to navigate though the site will always default to DE, thus the language will change after using the link

setting setdefaultlanguage with re-init current language

this is basically as if i don’t do anything


#8

Could you share your page creation code?
How is that method called? (Frontend / Backend / Router…)
If we solve the creating problem the rest will work automatically.


#9
//... 
$session->set('cart',Kirby\Toolkit\Str::random('24','alpha'));
//...

function draftOrder(){
// ... Some things previously like checking if month/year folders exist
// ... i have shorted the code a bit, so don't tell me there's a missing bracket...

    if(!getDraft($session->get('cart'))){ // checking if a draft is already existing

            if(!site()->page('shop/checkout/orders/'.$year.'/'.$month)->findPageOrDraft($session->get('cart'))){
                $draft = site()->page('shop/checkout/orders/'.$year.'/'.$month)->createChild([
                    'content' => array(
                        'title' => 'New Order',
                        'key' => Kirby\Toolkit\Str::random('16','alpha'),
                        'log' => yaml::encode(array(
                            'date' => date('y-m-D H:i',strtotime('NOW')),
                            'task' => t('cart.created'),
                        ))
                    ),
                    'slug' => $session->get('cart'),
                    'template' => 'shop-order',
                ], kirby()->defaultLanguage()->code());
            }
    }
   $draft = getDraft($session->get('cart'));      
   return $draft; 
}
function getDraft($draft){
// this is used to get the draft page from the saved session uid, so the first argument is passed 

    $page = null;
    $session = kirby()->session();
    if($yearPage = site()->page('shop/checkout/orders')->find($session->get('shop.date')['year'])){
        if($monthPage = $yearPage->find($session->get('shop.date')['month'])){
                // $currentlang = kirby()->language()->code();
                // kirby()->setCurrentLanguage(kirby()->defaultLanguage()->code());
                $page = $monthPage->draft($draft);
        }
    }
    return $page;
}

all functions are declared within index.php / or as include,
the functions usually get called in controller

// within a controller
    $draft = draftOrder();
   // do somethings

#10

createChild([...]); does not take language as second argument but that should not change the outcome.
I don’t see why both language files are created, the language is not involved in creating a page.

My last guess: page.create hooks (maybe plugins) that update the draft after creation?


#11

that pointed me to the right direction…

the plugin https://github.com/bnomei/kirby3-autoid

seems to be the issue here, while on this test site, i haven’t been using the plugin i kept it in the plugins folder, and it’s writing an invisible field to each page by default so i assume once my draft was created, autoid did it’s magic and created the other language version…

haven’t thought about other plugins being the cause.


#12

@bnomei you might want to pick this up as an issue. Check for content file to exist before updating and use default lang as fallback…or so :slight_smile:


#13

@moeli @carstengrimm
please sum up your issue with autoid & multilang and submit it to my repo. i currently do not have the time to read this thread and otherwise it might be forgotten.
what exactly are you doing? what did you expect? did it crash or create corrupted data? why is the problem an issue specific to autoid?
thanks.


#14

#15

i gave this some though since @carstengrimm reminded me this issue persists.

but i need help by some core devs like @distantnative to clear this up once and for all.

i think the problem is that:

  • page is edited and saved in panel in non default language
  • hook page update after kicks in which triggers autoid
  • autoid does it thing and calls page update to save the assigned autoid to page (only once not an endless loop)
  • problem is that the hook is aware of the current language thus autoid persist that.
  • usually the autoid value is not needed to be translated (or i could enforce that in a new release) and autoid could call page->update with default language as param. @distantnative do you think this will solve the issue?

#16

Might need different behavior for structure fields, though?


#17

@bastianallgeier sorry to pull you into this thread by force. could you please help me clear up this issue. the problem is beyond my knowledge of how the hooks work in relation to the panel and multilanguage setup. please see summary two posts above this one.