Disable add button for secondary languages

Hi,

Is it possible to disable the “Add” new page button for secondary languages?

There is no option for creating or deleting languages yet but you can hide secondary languages section with custom css:

.k-languages section + section {
    display: none;
}

Thanks! Will take a look.

The reason why I’m confused:

Creating a blog post with the primary language, add some text, save and publish. Then switching to a secondary language and the text is there, all good :+1: :+1:

Now creating a blog post with a secondary language, save and publish. Then switch back to primary language and there is no text, main text field is empty :thinking: :face_with_monocle: :face_with_monocle:

This was really confusing, two different results just because I started to create the post with a language other than the primary one.

Another really confusing thing:
I have a “publication date” field that is set to translate: false because I don’t want the user to be able to change this date for other languages. It should be the same for all of them. I also have other required fields and if you create a post in a secondary language, they’re all marked in red and can’t be changed.

This is the reason why I want to disable the “Add” button for secondary languages. I haven’t found a way to get around this confusion.

Your css didn’t work. There’s no k-languages class :thinking:

The dom seems identical between primary and secondary languages.

Hmm worked for me, did you use custom css, right?

Yes, tried with other classes and worked :thinking:

But I don’t have any .k-languages class when checking the source.

ahh sorry i got it wrong. this css hides the second language.

Thanks for your help. That plugin got options to solve this. Currently I try not to install plugins, I’m hoping the low-hanging fruits will be eatable soon :slight_smile:

Curious though to know how others are working with multi-lang sites. From a design-perspective it’s confusing to have the option translate: false and then being able to create pages/posts from secondary languages.

You just want to add pages only from the primary language, right?

Yes that’s it.

I think there is no way to prevent this for now, but let’s ask @texnixe

Maybe it’s possible with a route:before hook that at least throws an error when a user tries to create a page from a non-default language.

However, there seems to be no language information in those routes, so maybe not. The page is only ever generated in the default language first, anyway.

I think for the moment the only thing you can do is add an info field that asks user to create new pages in the default language.

Edit: This plugin adds a language code to the page: https://github.com/mullema/k3-panel-view-extended

Via this language code, it should be possible to select the page add buttons and hide them.

1 Like

I think for the moment the only thing you can do is add an info field that asks user to create new pages in the default language.

That’s actually a good idea!

How do you hide the field for the primary language?

I think you can use a conditional info section/info field based on a hidden field with the language code

The little trick would at least work if you have only two languages:

  1. Add two new fields to your blueprint:
    fields:
      languageCode:
        type: hidden
      info:
        type: info
        text: >
          Create new pages only in default language
        when:
          languageCode: de # English is the default language
  1. Hook to update page with language codes on page creation:
  'hooks' => [
        'page.create:after' => function($page) {
          foreach (kirby()->languages() as $language) {
            $page->update([
              'languageCode' => $language->code()
            ], $language->code());
          }
        },
  ]

Now the info field will only be shown on the non-default (in this example: German) page.

An alternative to this approach: Use a separate blueprint for default and non-default languages

Or the plugin I suggested in my last post, with the plugin, you only need a bit of CSS instead of these hacks (ok, the plugin is probably also a hack)

And another option: A page.create.before hook:

    'hooks' => [
        'page.create:before' => function($page) {
            if ($page->kirby()->language() !== $page->kirby()->defaultLanguage()) {
              throw new Exception('Please switch to the default language before you create a new page.');
            }
        },
    ]

(Maybe instead or in conjunction with the info field)

1 Like

Wow this works great!

@texnixe would it be possible to add a similar message if a user clicks one “Add” on a structured field?

What do you want to achieve?

I have a structured field with quite a few settings inside but only one text field that the editor can translate.

Currently everything inside the structured field have translate: false except for the text field that can be translated. Everything works great, you create the field with the primary language, then you switch to the secondary language, open the item and change the text. You can’t change any settings using a secondary language, thus the module will look the same on both languages.

The only small issue I have is if the editor is already on a secondary language and clicks on “Add”. A new item is created, all settings disabled except for the text field. And when saved the fields that had translate: false gets a value “null” in the .txt file.

I was wondering if I could add a similar message “Please switch to the default language.” when clicking on the “Add” button.

Edit: Ok this worked great until further testing :slight_smile: Once you make changes to the structured field using a secondary language. And then switch back to the primary language, those new items wont be added to the secondary language. So I guess I won’t be able to use translate: false on those settings inside the structured field.